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

Álv*_*cía 4 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>
Run Code Online (Sandbox Code Playgroud)

真的是代码是样式触发器,setter属性是针对其他情况的,但是我添加的代码可以影响结果.

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

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

谢谢.

TMa*_*Man 14

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

       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的,所选项目背景颜色实际上是系统颜色,因此您必须覆盖资源中的SystemColor.通常我使用`<DataGrid.Resources>`所以这种样式只适用于DataGrid,而不适用于应用程序中的每个选定项.另外,我认为你只需要`HighlightBrushKey`,但我不记得了.`ControlBrushKey`无关紧要. (3认同)