<...">

How to change font color in WPF ListView cell depending on cell value?

<ListView ItemsSource="{Binding}" Name="myView"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding fieldA}" Header="Field A" /> <GridViewColumn DisplayMemberBinding="{Binding fieldB}" Header="Field B" /> </GridView> </ListView.View> </ListView> 

I would like to know how to format my list, so if object.fieldA == "apples", the font will be red in cell fieldA.

Thanks, boffins.

+6
source share
1 answer

You need to use the DataTemplate to display the fieldA value and add a trigger that changes the Foreground property for the given value.

See also Overview of Data Templates on MSDN.

 <GridViewColumn Header="Field A"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock x:Name="Txt" Text="{Binding fieldA}" /> <DataTemplate.Triggers> <DataTrigger Binding="{Binding fieldA}" Value="apples"> <Setter TargetName="Txt" Property="Foreground" Value="Red" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 
+6
source

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


All Articles