wpf datagrid 交替行着色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4792628/
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 alternate row coloring
提问by Robin Maben
I have tried this method.. without luck..
我试过这种方法..没有运气..
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
Is there a way to get the row Index? I have even tried
有没有办法获得行索引?我什至试过
<DataTrigger Binding="{Binding AlternationIndex}" Value="0">
<Setter Property="Foreground" Value="Green"></Setter>
</DataTrigger>
采纳答案by matthias.lukaszek
Unless already done, you have to set the AlternationCount property of DataGrid:
除非已经完成,否则您必须设置 DataGrid 的 AlternationCount 属性:
<DataGrid AlternationCount="2"
... />
You should additionally check whether the Foreground property is used for any Control in the DataGridRow. Try setting the Background property to test the alternation stuff.
您还应该检查 Foreground 属性是否用于 DataGridRow 中的任何控件。尝试设置背景属性来测试交替的东西。
回答by Robin Maben
Finally, this is what I ended up with for generically setting alternate row colors.
最后,这就是我一般设置备用行颜色的结果。
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="#FFF" />
<Setter Property="AlternationCount" Value="2" />
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#CCC"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#EEE"></Setter>
</Trigger>
</Style.Triggers>
</Style>
回答by Th3G33k
Try setting the alternating background like this:
尝试像这样设置交替背景:
AlternationCount="2" AlternatingRowBackground="Bisque"
回答by Alan392
Try this
尝试这个
<DataGrid AlternationCount="2"
AlternatingRowBackground="Salmon" ........
回答by rosta
Finally I used combination of Robin Maben and Th3G33k solution, because I want the alternation colour to override with my own, when some condition is met. Thanks both.
最后我使用了 Robin Maben 和 Th3G33k 解决方案的组合,因为当满足某些条件时,我希望交替颜色覆盖我自己的颜色。谢谢两位。
<DataGrid x:Name="gridCustomerOrderItems" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" AutoGenerateColumns="False"
AlternationCount="2"
IsReadOnly="True" CanUserReorderColumns="True"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<!--first alteraniting colour-->
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#EEE"></Setter>
</Trigger>
<!--then override with my own colour-->
<DataTrigger Binding="{Binding InvoiceSet}" Value="True">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>