WPF DataGrid AlternatingRowBackground 和 RowStyle 优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13914382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid AlternatingRowBackground and RowStyle precedence
提问by user1514042
How do I make my RowStyleto get applied after AlternatingRowBackground? I want items, having IsOrangeas trueto have Orangebackground regardless of alternating row background, which isn't the case currently.
我如何让我的RowStyle申请之后AlternatingRowBackground?我想要的物品,有IsOrange作为true有Orange背景,无论交替行背景,这是不是这种情况目前的。
XAML:
XAML:
<DataGrid Name="g"
AlternatingRowBackground="Blue"
AlternationCount="2"
...
SelectionMode="Single">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding IsOrange}" Value="Y">
<Setter Property="Background" Value="Orange" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
...
</DataGrid>
回答by LPL
It's not a bug. In a Styleyou can't override a local value set for the alternating row. That's why this will not work
这不是一个错误。在 a 中,Style您不能覆盖为交替行设置的本地值。这就是为什么这行不通
<DataGrid AlternatingRowBackground="Blue"
But if you set AlternatingRowBackgroundin a Styleyou can
但是如果你设置AlternatingRowBackground在 aStyle你可以
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Blue"/>
</Style>
</DataGrid.Style>
Thanks to this answer.
感谢这个答案。
回答by Heysem Katibi
In my program I have two classes in addition to main window that contains one DataGird only. Let's start with first class:
在我的程序中,除了仅包含一个 DataGird 的主窗口之外,我还有两个类。让我们从头等舱开始:
MyClass.cs:
MyClass.cs:
public class MyClass
{
public bool IsOrange { get; set; }
public string Name { get; set; }
}
I have only two properties, IsOrangespecifies whether the row should be orange.
((Do not care for the other property.))
我只有两个属性,IsOrange指定行是否应为橙色。((不关心其他财产。))
Now the view model class contains only collection of MyClass.
现在视图模型类只包含 MyClass 的集合。
MyClassViewModel.cs:
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:
在 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:
最后在 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 email to send you the app.
您可以将您的电子邮件发送给我,以便向您发送应用程序。
Good Luck :)
祝你好运 :)

