屏幕上不可见的行将无法使用此方法着色,因为它们已被虚拟化并且实际上并不存在。在下面的样式中,我绑定到属性 IsRed 以将行转换为红色和它们的默认颜色(将其放在带有数据网格的 from 的资源中)
<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>
我的表单上有一个名为 IsRed 的依赖属性,这也可以是实现 INotifyPropertyChanged 的任何属性(依赖属性通知它们的更改)
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));
然后在我的xaml中,我在顶部有声明
<Window
x:Class="Grids.Window1"
x:Name="self">
这意味着我可以使用元素名称绑定来引用它(我觉得很有用的一种技术)
使用我概述的代码,您只需单击所有按钮即可
private void Button_Click(object sender, RoutedEventArgs e) {
IsRed = !IsRed;
}