Reputation: 2142
I have a method which comparing data in two dataTable. After comparing I´d like to visualizing these compared data (which are in new DataTable called ChangeTab) in WPF dataGRID.
I´m filling datagrid from DataContext:
win.TableOfChange.DataContext = ChangeTab.DefaultView;
<DataGrid ItemsSource="{Binding}" Height="107" HorizontalAlignment="Left"....
I´m fighting with problem, how to visualize data which are different (in the DataTables) by red, and data which are the same with green color - similar like is in the picture:
Is there is a way, how to do that (Set colour to WPF dataGRID cell for some red and some green from C#)?
Thanks a lot!
Upvotes: 0
Views: 2394
Reputation: 3049
I tend to use DataTemplates for most columns in the grid. You would need to create a class, something like this:
class CellContent
{
public object Content { get; set; }
public bool IsDifferent { get; set; }
}
Then create a datatemplate something like this:
<DataTemplate x:Key="bob">
<ContentPresenter Content="{Binding Path=Content}" TextElement.Foreground="{Binding Path=IsDifferent, Converter={StaticResource myConverter}}" />
<DataTemplate>
You then assign the template to each column.
The other option which is probably better would be to use a Trigger to change the colour if the IsDifferent is true.
Upvotes: 0
Reputation: 185553
I presume that you build the source table in the comparison process, if so you can easily store a boolean value that indicates equality. Then in the CellStyle
you can use a DataTrigger
on that property and have a Setter
change the TextElement.Foreground
property to the desired value.
Upvotes: 1