Search code examples
wpfxamldatagridresourcedictionary

WPF - DataGrid "inline" style resets base template


I have a rather complex application so I use a ResourceDictionary to manage all my styles and so one. There I have defined a style for DataGridColumnHeaderBaseStyle:

<Style x:Key="DataGridColumnHeaderBaseStyle" TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="Background" Value="{StaticResource TableHeaderBackgroundColor}" />
    <Setter Property="BorderBrush" Value="{StaticResource GridLinesColor}" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />
    <Setter Property="Padding" Value="{DynamicResource HeaderPadding}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid>
                    <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter Grid.Column="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <Path x:Name="SortArrow" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Width="8" Height="6"
                                  Margin="3,0,0,0" Stretch="Fill" Opacity="0.5" Fill="White" RenderTransformOrigin="0.5,0.4" Visibility="Collapsed"
                                  Data="M0,0 L1,0 0.5,1 z" />
                        </Grid>
                    </Themes:DataGridHeaderBorder>
                    <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="SortDirection" Value="Ascending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        <Setter TargetName="SortArrow" Property="RenderTransform">
                            <Setter.Value>
                                <RotateTransform Angle="180" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="SortDirection" Value="Descending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{StaticResource DisabledTableHeaderBackgroundColor}" />
            <Setter Property="BorderBrush" Value="{StaticResource DisabledGridLinesColor}" />
        </Trigger>
    </Style.Triggers>
</Style>

So far so good. In my main app i have a DataGrid that loads that style. The problem is when I add a Setter for a tooltip, I lose that style template for that column.

<DataGrid.Columns>
    <DataGridTextColumn Header="#" Binding="{Binding Number}" IsReadOnly="True" />
    <DataGridComboBoxColumn Header="Region" ItemsSource="{Binding Kiln.Regions, Source={StaticResource MainViewModel}}"
                        SelectedValueBinding="{Binding RegionNumber}" SelectedValuePath="Number" DisplayMemberPath="Name"
                        EditingElementStyle="{StaticResource EditingComboBoxStyle}" />
    <DataGridCheckBoxColumn Header="Has Mixture" Binding="{Binding HasMixture}" />
    <DataGridCheckBoxColumn Header="Has Burners" Binding="{Binding HasBurners}" />
    <DataGridTextColumn Header="Burners/Circuits" Binding="{Binding CircuitBurnersNumber}" />
    <DataGridTextColumn Header="GP ⌀" Binding="{Binding GasPipeDiameter}" >
        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ToolTip" Value="Gas Pipe ⌀ (mm)" />
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>
    <DataGridTextColumn Header="Gas Orifice ⌀ (mm)" Binding="{Binding GasOrificeDiameter}" />
    <DataGridTextColumn Header="Air Pipe ⌀ (mm)" Binding="{Binding AirPipeDiameter}" />
    <DataGridTextColumn Header="Air Orifice ⌀ (mm)" Binding="{Binding AirOrificeDiameter}" />
</DataGrid.Columns>

Adding that ignores the rest of the style. I'm I missing something? Any tip to solve this?

DataGrid

Here you can see the behavior I'm talking about. Thanks in advance.


Solution

  • new style should derive from base style, using BasedOn property:

    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader"
               BasedOn="{StaticResource DataGridColumnHeaderBaseStyle}">
            <Setter Property="ToolTip" Value="Gas Pipe ⌀ (mm)" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
    

    this way new style can add more setters or override some setters from base style