c# - 无法将类型为 'MS.Internal.NamedObject' 的对象转换为 BitmapImage

标签 c# wpf

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

Unable to cast object of type 'MS.Internal.NamedObject' to type '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 中进行必要的更改后,会抛出相同的错误,如下所示

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

最佳答案

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

您应该尝试修改您的绑定(bind)以从您的 View 模型中获取值,例如:

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

关于c# - 无法将类型为 'MS.Internal.NamedObject' 的对象转换为 BitmapImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866308/

相关文章:

javascript - 将 Razor 页面显示为模式弹出窗口

c# - 将代码放在依赖属性的 setter block 中是否安全,还是我应该始终使用回调方法?

WPF TextBox - 以编程方式选择文本,同时保留 "selection direction"

wpf - MVVM 模式是 MVC + PAC 模式的共生体吗?

c# - 返回后代的单例实例

C# winform,如何使轨迹条随着声音文件移动(增加其值)?

c# - LINQ-to-SQL 与 SQLite : syntax error near "SELECT" when inserting

c# - 无法更新 EntitySet '...' 因为它有一个 DefiningQuery

C# 在 WebBrowser 中调用 JavaScript

c# - 如何正确绑定(bind)MVVM中的Image.Source?