You can use DataTrigger for this.
Here is a small example. I created a class called Person with the Name, Age, and Active properties.
public class Person { public string Name { get; set; } public int Age { get; set; } public bool Active { get; set; } }
In the designer of the main window, I add 3 Person objects to the list, and then bind this list to the DataGrid .
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<Person> people = new List<Person>(); people.Add(new Person() { Name = "John Doe", Age = 32, Active = true }); people.Add(new Person() { Name = "Jane Doe", Age = 30, Active = true }); people.Add(new Person() { Name = "John Adams", Age = 64, Active = false }); tblLog.ItemsSource = people; } }
Then in XAML for MainWindow, I create a DataTrigger style as a resource.
<Window.Resources> <Style TargetType="DataGridRow"> <Style.Triggers> <DataTrigger Binding="{Binding Active}" Value="False"> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Window.Resources>
What this trigger does is the value from the Active field from the Person object that is in the DataGridRow, and if this value is false, then it goes into the background color of the row before red.
Ryan alford
source share