如何更改所选数据网格行的蓝色

时间:2012-07-27 18:56:16

标签: wpf datagrid

好吧,我已经看到了这样做的代码:

<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>

真的代码是样式触发器,setter属性适用于其他情况,但我添加的代码可能会影响结果。

我想更改选定的行背景,默认为蓝色,我想根据某些条件使用其他颜色。例如,如果添加了一个寄存器,那么如果我选择该行它将为绿色,如果未选中该行,则它将为浅绿色。

我可以根据需要更改行的颜色,但是当我选择它时,总是蓝色。

感谢。

2 个答案:

答案 0 :(得分:14)

你可以尝试这样的事情......

       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />

答案 1 :(得分:0)

为此您需要以编程方式指定行的颜色

<Setter Property="Background" Value="{Binding Path=BgColor}"/>  

BgColor 属性添加到绑定到网格的对象。在你的条件设置 BgColor (即如果对象是注册,那么BgColor是“绿色”)

相关问题