无法投类型的对象MS.Internal.NamedObject“来的BitmapImage [英] Unable to cast object of type 'MS.Internal.NamedObject' to BitmapImage

查看:571
本文介绍了无法投类型的对象MS.Internal.NamedObject“来的BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立在我得到一个错误



WPF应用程序

无法转换类型MS.Internal的'对象。 NamedObject为键入'System.Windows.Media.Imaging.BitmapImage




XAML代码

 < D​​ataGridTemplateColumn标题=活动WIDTH =60> 
< D​​ataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<按钮和GT;
< Button.Content>
< MultiBinding转换器={StaticResource的myImageConverter}
ConverterParameter =活动>
<绑定路径=isClosed返/>
<绑定路径=器isChecked/>
<绑定路径=IsActive/>
<绑定路径=TickImage/>
<绑定路径=CrossImage/>
< / MultiBinding>
< /Button.Content>
< /按钮>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>





C#转换代码

 公共类ImageConverter:IMultiValueConverter 
{
公共对象转换(对象[]值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
布尔isClosed返=(布尔)值[0];
布尔=器isChecked(布尔)值[1];
BOOL isActive =(布尔)值[2];
图片IMG =新的图像();

开关((字符串)参数)
{
案活动:
如果(isClosed返==真&放大器;&放大器;器isChecked ==真)
{
如果(isActive ==真)
{
img.Source =(BitmapImage的)值[3];
img.Stretch = Stretch.Fill;
}
,否则
{
img.Source =(BitmapImage的)值[4];
img.Stretch = Stretch.Fill;
}
}
中断;
}

返回IMG;
}

公共对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,System.Globalization.CultureInfo文化)
{
抛出新NotImplementedException( );
}
}



TickImage和CrossImage是视图模型类的属性。他们被初始化在如下图所示的视图模型构造

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



在isClosed,器isChecked和IsActive是数据对象类的属性。



在条件一号线出现的错误如果(isActive == true)而



我也曾尝试以下XAML代码:

 < Button.Content> 
<图像>
< Image.Source>
< MultiBinding转换器={StaticResource的myImageConverter}
ConverterParameter =活动>
<绑定路径=isClosed返/>
<绑定路径=器isChecked/>
<绑定路径=IsActive/>
<绑定路径=TickImage/>
<绑定路径=CrossImage/>
< / MultiBinding>
< /Image.Source>
< /图像>
< /Button.Content>



TickImage和CrossImage是在视图模型,并作必要的修正简单的字符串中的转换器相同的错误被抛出如下:




无法转换类型'MS.Internal.NamedObject的对象为类型System.String



解决方案

我敢肯定,你的绑定是不正确的。当您执行 CellTemplate 您实际上是结合到细胞的DataContext的,而不是你的观点的DataContext的(即视图模型)。


$里面有约束力b $ b

您应该尝试修改您的绑定从您的视图模型采用的值,像这样的例子:

 < ;绑定路径=DataContext.TickImage的RelativeSource ={的RelativeSource AncestorType = DataGrid中}/​​> 


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'

解决方案

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}" />

这篇关于无法投类型的对象MS.Internal.NamedObject“来的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆