【问题标题】:Unable to cast object of type 'MS.Internal.NamedObject' to BitmapImage无法将“MS.Internal.NamedObject”类型的对象转换为 BitmapImage
【发布时间】:2013-09-22 20:42:59
【问题描述】:

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

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

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# 转换器代码

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 和 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 和 IsActive 是 DataObject 类的属性。

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

我也尝试过以下 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 和 CrossImage 是 ViewModel 中的简单字符串,如果在 Converter 中进行必要的更改,则会引发相同的错误,如下所示

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

【问题讨论】:

  • 请注意不用写if (isActive == true)。简单写成if (isActive)
  • 那么两行之后的img = (string)values[3]; 呢?这有意义吗?
  • 为什么不在有问题的行设置断点并检查 values[3] 中的内容?
  • img = (string)values[3]; 不是代码的一部分...我很抱歉...values[3]values[4] 包含 {DependencyProperty.UnsetValue}

标签: c# wpf


【解决方案1】:

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

您应该尝试修改绑定以从视图模型中获取值,例如:

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

【讨论】:

  • 我对 WPF 有点陌生,所以这听起来可能是一个愚蠢的问题,但为什么在 ViewModel 中定义 TickImage 时是 AncestorType=DataGrid...我的意思是 AncestorType 不应该是 UserControl ...(虽然代码适用于AncestorType 的两个值...我不知道如何)。
  • DataContext 是一个继承的依赖属性,因此 DataGrid 获得与 UserControl 相同的 DataContext(除非它在 ​​UserControl 和 DataGrid 之间的可视树中的某处显式更改)。您可以使用其中任何一种,但在这种情况下,使用 DataGrid 可以避免不必要地沿着可视化树往上走。
猜你喜欢
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多