Unable to pass object of type "MS.Internal.NamedObject" to BitmapImage

I am creating a WPF application in which I get an error like

Unable to pass an object of type "MS.Internal.NamedObject" to enter "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 the DataObject class.

The error occurs in the first line of the if (isActive == true) condition if (isActive == true)

I 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 the necessary changes in the converter, the same error occurs as follows

Unable to pass object like "MS.Internal.NamedObject" to enter "System.String"

+7
source share
1 answer

I am sure your bindings are incorrect. When you bind inside a CellTemplate , you are actually snapping to the DataContext cell, not your DataContext view (i.e., To the viewmodel).

You should try changing your bindings to take values โ€‹โ€‹from your view model, for example, for example:

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

Source: https://habr.com/ru/post/954043/


All Articles