wpf datagrid rowheader扩展器来自资源字典的togglebuttton样式

时间:2017-12-06 20:06:51

标签: c# wpf datagrid

我有一个带有RowDetails的WPF DataGrid,它使用RowHeader Expander来扩展/折叠RowDetails。 我试图将RowHeader Expander ToggleButton的样式设置为资源字典中定义的ToggleButton样式。我试过这个:

<DataGrid.RowHeaderTemplate>
<DataTemplate>
    <Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed" >
        <Style TargetType="ToggleButton" BasedOn="{StaticResource ExpandCollapseToggleStyle}" />
    </Expander>
</DataTemplate>

这会编译但不会改变ToggleButton的样式。

1 个答案:

答案 0 :(得分:0)

这并不容易。如果您强制Expander进入RowHeaderTemplate,则需要设置实际Expander对象的样式,以便对埋藏在其中的ToggleButton产生影响。

以下是Aero.Normal WPF主题中Expander对象的默认样式。以此为基础,您可以修改其中包含的ToggleButton

<Style x:Key="{x:Type Expander}"
       TargetType="{x:Type Expander}">
    <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background"
            Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment"
            Value="Stretch"/>
    <Setter Property="VerticalContentAlignment"
            Value="Stretch"/>
    <Setter Property="BorderBrush"
            Value="Transparent"/>
    <Setter Property="BorderThickness"
            Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Expander}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        CornerRadius="3"
                        SnapsToDevicePixels="true">
                    <DockPanel>
                        <ToggleButton x:Name="HeaderSite"
                                      DockPanel.Dock="Top"
                                      Margin="1"
                                      MinWidth="0"
                                      MinHeight="0"
                                      Style="{StaticResource ExpanderDownHeaderStyle}"
                                      FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}"
                                      IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                      Content="{TemplateBinding Header}"
                                      ContentTemplate="{TemplateBinding HeaderTemplate}"
                                      ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                      Foreground="{TemplateBinding Foreground}"
                                      Padding="{TemplateBinding Padding}"
                                      FontFamily="{TemplateBinding FontFamily}"
                                      FontSize="{TemplateBinding FontSize}"
                                      FontStyle="{TemplateBinding FontStyle}"
                                      FontStretch="{TemplateBinding FontStretch}"
                                      FontWeight="{TemplateBinding FontWeight}"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        <ContentPresenter x:Name="ExpandSite"
                                          DockPanel.Dock="Bottom"
                                          Visibility="Collapsed"
                                          Focusable="false"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Margin="{TemplateBinding Padding}"/>
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded"
                             Value="true">
                        <Setter Property="Visibility"
                                Value="Visible"
                                TargetName="ExpandSite"/>
                    </Trigger>
                    <Trigger Property="ExpandDirection"
                             Value="Right">
                        <Setter Property="DockPanel.Dock"
                                Value="Right"
                                TargetName="ExpandSite"/>
                        <Setter Property="DockPanel.Dock"
                                Value="Left"
                                TargetName="HeaderSite"/>
                        <Setter Property="Style"
                                Value="{StaticResource ExpanderRightHeaderStyle}"
                                TargetName="HeaderSite"/>
                    </Trigger>
                    <Trigger Property="ExpandDirection"
                             Value="Up">
                        <Setter Property="DockPanel.Dock"
                                Value="Top"
                                TargetName="ExpandSite"/>
                        <Setter Property="DockPanel.Dock"
                                Value="Bottom"
                                TargetName="HeaderSite"/>
                        <Setter Property="Style"
                                Value="{StaticResource ExpanderUpHeaderStyle}"
                                TargetName="HeaderSite"/>
                    </Trigger>
                    <Trigger Property="ExpandDirection"
                             Value="Left">
                        <Setter Property="DockPanel.Dock"
                                Value="Left"
                                TargetName="ExpandSite"/>
                        <Setter Property="DockPanel.Dock"
                                Value="Right"
                                TargetName="HeaderSite"/>
                        <Setter Property="Style"
                                Value="{StaticResource ExpanderLeftHeaderStyle}"
                                TargetName="HeaderSite"/>
                    </Trigger>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>