Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c# - Unable to cast object of type 'MS.Internal.NamedObject' to BitmapImage

I am building a WPF application in which I am getting an error as

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.Media.Imaging.BitmapImage'

XAML Code:

<DataGridTemplateColumn Header="Active" Width="60">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
       <Button>
            <Button.Content>
                <MultiBinding Converter="{StaticResource myImageConverter}"
                              ConverterParameter="Active">
                    <Binding Path="IsClosed"/>
                    <Binding Path="IsChecked"/>
                    <Binding Path="IsActive"/>
                    <Binding Path="TickImage"/>
                    <Binding Path="CrossImage"/>
                </MultiBinding>
            </Button.Content>
        </Button>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

C# Converter Code:

public class ImageConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool isClosed = (bool)values[0];
        bool isChecked = (bool)values[1];
        bool isActive = (bool)values[2];
        Image img = new Image();

        switch ((string)parameter)
        {
            case "Active":
                if (isClosed == true && isChecked == true)
                {
                    if (isActive == true)
                    {
                        img.Source = (BitmapImage)values[3];
                        img.Stretch = Stretch.Fill;
                    }
                    else
                    {
                        img.Source = (BitmapImage)values[4];
                        img.Stretch = Stretch.Fill;
                    }
                }
                break;
        }

        return img;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

TickImage and CrossImage are properties of the ViewModel Class. They are initialized in the ViewModel constructor as shown below.

TickImage = new BitmapImage(new Uri("K:\projects\ContentSets\Ownership\SOMA\Staging\SOMA\Images\icon_tick.gif", UriKind.Absolute));
CrossImage = new BitmapImage(new Uri("K:\projects\ContentSets\Ownership\SOMA\Staging\SOMA\Images\icon_cross.gif", UriKind.Absolute));
TickImage.Freeze();
CrossImage.Freeze();

IsClosed, IsChecked and IsActive are properties of DataObject Class.

The error occurs at the 1st line of the condition if (isActive == true)

I have also tried the following XAML code:

<Button.Content>
    <Image>
        <Image.Source>
            <MultiBinding Converter="{StaticResource myImageConverter}"
                  ConverterParameter="Active">
                <Binding Path="IsClosed"/>
                <Binding Path="IsChecked"/>
                <Binding Path="IsActive"/>
                <Binding Path="TickImage"/>
                <Binding Path="CrossImage"/>
            </MultiBinding>
        </Image.Source> 
    </Image>
</Button.Content>

TickImage and CrossImage are simple strings in the ViewModel and with necessary changes in the Converter the same error is thrown as follows

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.String'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm pretty sure your bindings are incorrect. When you perform a binding inside a CellTemplate you're actually binding to the cell's DataContext rather than your view's DataContext (i.e the viewmodel).

You should try modifying your bindings to take the values from your viewmodel, like this for example:

<Binding Path="DataContext.TickImage" RelativeSource="{RelativeSource AncestorType=DataGrid}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...