在WPF中覆盖样式值的正确方法

Jim*_*_CS 15 c# wpf xaml

我想编辑DataGridWPF中的单元格样式.所以使用Expression Blend我右转到 - 对象和时间轴>> DataGrid >>编辑其他模板>>编辑CellStyle >>编辑副本
这是页面上显示的内容:

<SolidColorBrush x:Key="{x:Static DataGrid.FocusBorderBrushKey}" Color="#FF000000"/>
<Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

但我只想改变填充和背景.相反,它给了我25行代码,包括单元格模板!我错过了什么,有没有更好的方式来设计这样的项目,而不必在我只想更改两个项目时带来如此多的额外不必要的代码?

Kor*_*fin 37

查看样式的" BasedOn "属性...

例如,以下样式从DataGridColumnHeader获取所有内容,并仅覆盖Horizo​​ntalContentAlignment属性:

<Style x:Key="CenterAlignedColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" 
       BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
Run Code Online (Sandbox Code Playgroud)