In my program, I have two classes in addition to the main window containing only one DataGird. Let's start with the first class:
MyClass.cs:
public class MyClass { public bool IsOrange { get; set; } public string Name { get; set; } }
I have only two properties, IsOrange indicates whether the string should be orange. ((Does not care about another property.))
Now the view model class contains only the MyClass collection.
MyClassViewModel.cs:
public class MyClassViewModel { public ObservableCollection<MyClass> con { get; set; } public MyClassViewModel() { con = new ObservableCollection<MyClass>(); con.Add(new MyClass { IsOrange = true, Name = "Aa" }); con.Add(new MyClass { IsOrange = true, Name = "Bb" }); con.Add(new MyClass { IsOrange = false, Name = "Cc" }); con.Add(new MyClass { IsOrange = false, Name = "Dd" }); con.Add(new MyClass { IsOrange = false, Name = "Ee" }); con.Add(new MyClass { IsOrange = true, Name = "Ff" }); con.Add(new MyClass { IsOrange = true, Name = "Gg" }); con.Add(new MyClass { IsOrange = false, Name = "Hh" }); } }
In MainWindow.xaml:
<Grid> <DataGrid Margin="10" ItemsSource="{Binding Path=con}" > <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsOrange}" Value="true"> <Setter Property="Background" Value="Orange" /> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> </DataGrid> </Grid>
Finally, in MainWindow.xaml.cs:
public partial class MainWindow : Window { MyClassViewModel VM = new MyClassViewModel(); public MainWindow() { InitializeComponent(); DataContext = VM; } }
and this is the result:

You can send me your letter to send you the application.
Good luck :)
MoHaKa
source share