C# DataGridRowStyle被RowHighlightBrush覆盖

C# DataGridRowStyle被RowHighlightBrush覆盖,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个DataGridStyle,它基于一个名为ApprovalLevel的值修改TextBlock的前台属性。看起来是这样的: <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <DataTrigger Binding="{Binding ApprovalLevel}" Value="-1"> <Setter Property="For

我有一个DataGridStyle,它基于一个名为ApprovalLevel的值修改TextBlock的前台属性。看起来是这样的:

<Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ApprovalLevel}" Value="-1">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ApprovalLevel}" Value="0">
            <Setter Property="Foreground" Value="Black"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ApprovalLevel}" Value="1">
            <Setter Property="Foreground" Value="Green"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ApprovalLevel}" Value="2">
            <Setter Property="Foreground" Value="Blue"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

这会将样式应用于行而不会出现问题,但是当用户高亮显示该行时,样式将被覆盖,前景颜色将丢失。我已尝试应用以下样式:

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


但是,所有这些都会覆盖整行,使其透明。我的问题是如何让用户清楚地知道他们选择了一行,同时保留了我要应用的前景样式?

使用
ElementStyle
而不是使用
DataGridRow
样式设置
TextBlock
的样式:

<DataGrid x:Name="dg">
    <DataGrid.Resources>
        <Style x:Key="style" TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ApprovalLevel}" Value="-1">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ApprovalLevel}" Value="0">
                    <Setter Property="Foreground" Value="Black"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ApprovalLevel}" Value="1">
                    <Setter Property="Foreground" Value="Green"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ApprovalLevel}" Value="2">
                    <Setter Property="Foreground" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding YourProperty}" ElementStyle="{StaticResource style}" />
        <!-- ... -->
    </DataGrid.Columns>
</DataGrid>


谢谢你的回答。我将如何修改HighlightedRowColor,因为这种样式似乎覆盖了我以前使用的方法?请澄清您的要求。你想换什么颜色?@CBreeze:请记住投票选出有用的答案: