wpf 如何更改选定数据网格行的蓝色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11694022/
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
How to change the blue color of selected datagrid rows
提问by álvaro García
Well, I have seen this code to do that:
好吧,我已经看到这段代码可以做到这一点:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="Green">
</Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource ucComponentesColorFilaMultiValueConverter}">
<Binding ElementName="dgdComponentes" Path="ItemsSource" />
<Binding ElementName="dgdComponentes" Path="SelectedItems" />
<Binding ElementName="CurrentItem" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
Really the code is the style trigger, the setter property is for other cases, but I add the code of this can affect to the result.
确实代码是样式触发器,setter 属性用于其他情况,但我添加了此代码会影响结果。
I would like to change the selected row background, that by default is blue, and I want to other color according to some conditions. For example, if a register is added, then if I select the row it would be green, if this row is unselected, it would be light green.
我想更改所选行的背景,默认情况下为蓝色,我想根据某些条件更改其他颜色。例如,如果添加了一个寄存器,那么如果我选择该行,它将是绿色的,如果未选择该行,它将是浅绿色。
I am be able to change the color of the rows as I want, but when I select it, always is blue.
我可以根据需要更改行的颜色,但是当我选择它时,总是蓝色。
Thanks.
谢谢。
回答by TMan
You can try something like this maybe...
你可以尝试这样的事情也许...
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
回答by Anand
for this you need to assign color of the row pro-grammatically
为此,您需要以编程方式分配行的颜色
<Setter Property="Background" Value="{Binding Path=BgColor}"/>
Add the BgColorproperty to the object that is being bound to the grid. On your condition set BgColor(i.e if object is register then BgColor is "Green")
将BgColor属性添加到绑定到网格的对象。根据您的条件设置BgColor(即,如果对象已注册,则 BgColor 为“绿色”)

