Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid icon and boolean value

I have:

public class Person 
{
   String name { get; set; }
   String address { get; set; } 
   bool isMarried { get; set; } 
}

My datagrid gets populated with a list of persons.

I want to have a custom column where icon-1.jpg is displayed when isMarried is true and icon-2.jpg is displayed when isMarried is false.

How do I do this in WPF ? Any ideas ?

I know how to do a custom column but I do not know how to assoc the two states of isMarried with icon-1.jpg and icon-2.jpg.

like image 669
MadSeb Avatar asked Jul 19 '26 16:07

MadSeb


1 Answers

You could do this with a DataTrigger in your custom column:

<DataGridTemplateColumn Header="Married">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <Image x:Name="IMG" Source="married_image" /> 
         <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=isMarried}" Value="False">
               <Setter Property="Source" Value="not_married_image" TargetName="IMG"/>
            </DataTrigger>
         </DataTemplate.Triggers>
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
like image 149
John Myczek Avatar answered Jul 22 '26 04:07

John Myczek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!