无法将"MS.Internal.NamedObject"类型的对象强制转换为BitmapImage

Soh*_*amC 10 c# wpf

我正在构建一个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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

IsClosed,IsChecked和IsActive是DataObject类的属性.

错误发生在条件的第1行 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>
Run Code Online (Sandbox Code Playgroud)

TickImage和CrossImage是ViewModel中的简单字符串,并且在Converter中进行了必要的更改,同样的错误被抛出如下

无法将"MS.Internal.NamedObject"类型的对象强制转换为"System.String"类型

Adi*_*ter 7

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

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

<Binding Path="DataContext.TickImage" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
Run Code Online (Sandbox Code Playgroud)

  • “DataContext”是继承的依赖属性,因此 DataGrid 获得与 UserControl 相同的 DataContext(除非它在 ​​UserControl 和 DataGrid 之间的可视化树中的某个位置显式更改)。您可以使用其中任何一个,但在这种情况下,使用“DataGrid”可以避免不必要地在可视化树上进一步走动。 (2认同)