2011-12-21 65 views
13

我想編輯WPF中的DataGrid的單元格樣式。因此,使用Expression Blend中我的權利去 - 對象和時間線>>的DataGrid >>編輯其他模板>>編輯CellStyle >>編輯複製
這裏是出現在頁面上的什麼什麼:正確的方式來覆蓋WPF中的樣式值

<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行代碼,包括單元格模板!我是否錯過了一些東西,有沒有更好的方式來設計這樣的項目,而不必帶上太多額外的不必要的代碼,當我只想改變兩個項目?

回答

30

時退房「支持算法FMP」屬性樣式...

例如下面的樣式採取一切從DataGridColumnHeader只有:基於您發佈的默認模板下面覆蓋Horizo​​ntalContentAlignment property:

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

很棒的功能。幫助我使用插件dll而沒有它搞亂了我的風格爲我節省了大量的工作。謝謝! – 2015-03-26 21:23:19

2

WPF中的覆蓋控件模板要求您完全替換模板。您可能只想更改模板的一個方面,但結果是表達式轉儲模板其餘部分的副本,以便可以覆蓋它。確保你以正確的方式重寫了單元格(我不確定是否有另一種方法)。某些控件(想到的是ListView)可以讓您在不覆蓋整個控件模板的情況下更換數據模板,但我不確定這是您想要的,還是可以使用DataGrid完成。

見這個問題的答案:Replace part of default template in WPF

2

做你想做的事,你通常會剛剛成立的背景和填充性的風格:

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