Boolean to print image, just print the image name C #
I have a DataGridTextColumn containing a boolean, these booleans return TRUE or FALSE on the column.
I want to replace this with two images, one for TRUE, one for FALSE.
this is the DataGrid:
<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=ResultatCollectionGrande}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="PrisEnCompte" Width="50" Binding="{Binding Path=Flag, Converter={StaticResource BooleanConverter}}" Header="PEC"></DataGridTextColumn>
with resource:
<Window.Resources>
<local:BooleanConverter x:Key="BooleanConverter"/>
</Window.Resources>
this is the converter:
public class BooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.Equals(true)) return @"booleanTrue.png";
return @"booleanFalse.png";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
The converter works, it changes what my DataGridTextColumn displays. It now displays "booleanTrue.png" for TRUE and "booleanFalse.png", but does not display images.
How can I show what I am missing to print these images in my DataGrid?
Thanks in advance for your help.
source to share
Instead of using the DataGridTextColumn, you need a Template column to display the image in that column, and apply the binding only on that template column. let me show you the way
Your datagrid should look like this
<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=ResultatCollectionGrande}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTemplateColumn Header="PEC" x:Name="PrisEnCompte">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="50" Width="50" Source="{Binding Path=Flag, Converter={StaticResource booleanConverter}}"></Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
and your Boolean converter should look like this.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.Equals(true))
return new Uri(@"C:\Users\pj827192\Desktop\Untitled.png");
return new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
source to share