wpf 无法将“MS.Internal.NamedObject”类型的对象转换为 BitmapImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18866308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:40:21  来源:igfitidea点击:

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

c#wpf

提问by SohamC

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

我正在构建一个 WPF 应用程序,其中出现错误

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

无法将“MS.Internal.NamedObject”类型的对象转换为“System.Windows.Media.Imaging.BitmapImage”

XAML Code:

XAML 代码

<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:

C# 转换器代码

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 和 CrossImage 是 ViewModel 类的属性。它们在 ViewModel 构造函数中初始化,如下所示。

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.

IsClosed、IsChecked 和 IsActive 是 DataObject 类的属性。

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

错误发生在条件的第一行 if (isActive == true)

I have also tried the following XAML code:

我还尝试了以下 XAML 代码:

<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

TickImage 和 CrossImage 是 ViewModel 中的简单字符串,在 Converter 中进行必要的更改后,会抛出相同的错误,如下所示

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

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

采纳答案by Adi Lester

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

我很确定您的绑定不正确。当您在 a 中执行绑定时,CellTemplate您实际上是绑定到单元格的 DataContext 而不是视图的 DataContext(即视图模型)。

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