The rows that are not visible on the screen won't be abled to be coloured using this method as they are virtualised away and dont actually exist. In the style below im binding to a property IsRed to turn the rows between red and their default colour (put this in the resources of the from with the datagrid on it)
<Style
TargetType="{x:Type dg:DataGridRow}"
BasedOn="{StaticResource {x:Type dg:DataGridRow}}">
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=self, Path=IsRed}"
Value="True">
<Setter
Property="Background"
Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
i have a dependency property on my form called IsRed, this could also be any property that implements INotifyPropertyChanged (dependency properties notify about their changes)
public Boolean IsRed {
get { return (Boolean)GetValue(IsRedProperty); }
set { SetValue(IsRedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsRed. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsRedProperty =
DependencyProperty.Register("IsRed", typeof(Boolean), typeof(Window1), new UIPropertyMetadata(false));
then in my xaml i have the declaration at the top
<Window
x:Class="Grids.Window1"
x:Name="self">
which means i can reference it with an element name binding (a technique i find useful)
With the code as ive outlined all your button click would have to do would be
private void Button_Click(object sender, RoutedEventArgs e) {
IsRed = !IsRed;
}