如何将数据网格行的背景绑定到特定颜色? [英] How do I bind the background of a data grid row to specific color?
本文介绍了如何将数据网格行的背景绑定到特定颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可观察的集合绑定到数据网格。我也在视图模型中有一个颜色属性,我想将数据网格中每行的背景绑定到vm上的颜色属性。
I have a observable collection that binds to a data grid. I also have in the view model a color property and I want to bind the background of each row in the data grid to the color property on the vm.
推荐答案
您可以在 RowStyle
中绑定背景
DataGrid
<DataGrid ...>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding MyBackground}"/>
</Style>
</DataGrid.RowStyle>
<!-- ... -->
</DataGrid>
如果 MyBackground
是一个画笔
。你在你的问题中提到你有一个颜色
,如果是这样的话,你可以使用它
This will work if MyBackground
is a Brush
. You mention in your question that you have a Color
, if this is the case you can use this instead
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding MyColor}"/>
</Setter.Value>
</Setter>
这篇关于如何将数据网格行的背景绑定到特定颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文