Wpf 如何在字典中为所有数据网格设置ControlBrushKey?

Wpf 如何在字典中为所有数据网格设置ControlBrushKey?,wpf,datagrid,resourcedictionary,Wpf,Datagrid,Resourcedictionary,我有这本字典: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="{x:Type StackPanel}"> <Setter Pro

我有这本字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type StackPanel}">
        <Setter Property="Margin" Value="12,12,20,20"/>
    </Style>

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="IsReadOnly" Value="true"/>
        <Setter Property="CanUserAddRows" Value="false"/>
        <Setter Property="CanUserDeleteRows" Value="true"/>
        <Setter Property="AutoGenerateColumns" Value="false"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Visible"/>
    </Style>


    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="SkyBlue" />
        </Style.Resources>
    </Style>

在我的视图中,我加载字典,dataGrid获得dataGrid的属性,但它们不使用DataGridRow的属性

我想知道如何设置DataGridRows的属性,因为我想在dataGrid没有焦点时更改所选行的颜色,因为defaulr灰色不好


谢谢。

您可以在样式触发器中使用
DataGridRow
IsSelected
属性,如下所示。然后,您可以在选定行时修改其颜色

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Background" Value="SkyBlue"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="SkyBlue" />
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="SkyBlue"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="BorderBrush" Value="Tomato" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers>
</Style>