WPF - DataGrid"内联"样式重置基础模板

时间:2018-03-09 14:43:58

标签: c# wpf wpfdatagrid resourcedictionary

我有一个相当复杂的应用程序,因此我使用ResourceDictionary来管理我的所有样式。在那里我为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>

到目前为止一切顺利。在我的主应用程序中,我有一个加载该样式的DataGrid。问题是当我为工具提示添加Setter时,我丢失了该列的样式模板。

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

添加它会忽略其余的样式。我错过了什么?有任何提示可以解决这个问题吗?

DataGrid

在这里你可以看到我正在谈论的行为。提前谢谢。

1 个答案:

答案 0 :(得分:2)

新样式应该来自基本样式,使用BasedOn属性:

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

这样新的样式可以添加更多的setter或覆盖基本样式的一些setter