【问题标题】:Proper way to override style values in WPF在 WPF 中覆盖样式值的正确方法
【发布时间】:2011-12-21 18:54:45
【问题描述】:

我想在 WPF 中编辑 DataGrid 的单元格样式。所以使用 Expression Blend 我直接去 - 对象和时间线>>DataGrid>>编辑其他模板>>编辑单元格样式>>编辑副本
以下是页面上显示的内容:

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

但我只想更改填充和背景。相反,它给了我 25 行代码,包括单元格模板!我是否遗漏了一些东西,当我只想更改两个项目时,有没有更好的方法来设计这样的项目,而不必带来太多额外的不必要的代码?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    查看样式的“BasedOn”属性...

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

    <Style x:Key="CenterAlignedColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" 
           BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
    

    【讨论】:

    • 很棒的功能。帮助我使用插件 dll 而不会弄乱我的风格,从而为我节省了大量工作。谢谢!
    【解决方案2】:

    在 WPF 中覆盖控件模板需要您完全替换模板。您可能只想更改模板的一个方面,但结果是表达式转储模板其余部分的副本,以便可以覆盖它。确保您以正确的方式覆盖单元格(我不确定是否有另一种方式)。一些控件(想到ListView)可以让您在不覆盖整个控件模板的情况下交换数据模板,但我不确定这是否是您想要的,或者是否可以使用DataGrid 完成。

    查看答案:Replace part of default template in WPF

    【讨论】:

      【解决方案3】:

      要做你想做的事,你通常只需在一个样式中设置背景和填充属性:

      <Style TargetType="DataGridCell">
          <Setter Property="Padding" Value="10" />
          <Setter Property="Background" Value="Green" />
      </Style>
      

      但是在这种情况下,DataGridCell 的默认控件模板似乎忽略了填充值,因此您需要将其替换为没有的实现。以下内容基于您发布的默认模板:

      <Style TargetType="DataGridCell">
          <Setter Property="Padding" Value="10" />
          <Setter Property="Background" Value="Green" />
          <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="{x:Type DataGridCell}">
                  <Border BorderBrush="{TemplateBinding BorderBrush}" 
                          BorderThickness="{TemplateBinding BorderThickness}" 
                          Background="{TemplateBinding Background}" 
                          SnapsToDevicePixels="True">
                      <ContentPresenter 
                          Margin="{TemplateBinding Padding}" <!-- this bit does the padding -->
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                  </Border>
              </ControlTemplate>
          </Setter.Value>
          </Setter>
      </Style>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 2012-01-18
        • 1970-01-01
        • 2016-10-19
        • 2018-09-14
        • 2023-04-10
        • 2015-12-18
        相关资源
        最近更新 更多