为什么当我设置模板时我的 Expander 没有展开?

发布于 2024-10-11 11:20:28 字数 25985 浏览 2 评论 0原文

我正在尝试更改 Expander 的展开按钮的颜色,似乎唯一的方法是更改​​其控件模板,因为没有属性可以设置它。

因此,我使用 向我展示模板 来获取 Aero 主题的模板。然后,我在 Expander 上设置了该模板,没有进行其他更改。它看起来不错,但单击按钮不再导致它展开。

谁能解释为什么会发生这种情况?我希望这很简单,但我仍在学习 WPF 并且还无法弄清楚。谢谢。

这是完整的代码:

<Expander Header="Test">
  <Expander.Template>
    <ControlTemplate TargetType="Expander" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Border BorderThickness="{TemplateBinding Border.BorderThickness}" CornerRadius="3,3,3,3" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
        <DockPanel>
          <ToggleButton IsChecked="False" Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" Foreground="{TemplateBinding TextElement.Foreground}" FontFamily="{TemplateBinding TextElement.FontFamily}" FontSize="{TemplateBinding TextElement.FontSize}" FontStretch="{TemplateBinding TextElement.FontStretch}" FontStyle="{TemplateBinding TextElement.FontStyle}" FontWeight="{TemplateBinding TextElement.FontWeight}" HorizontalContentAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding Control.VerticalContentAlignment}" Padding="{TemplateBinding Control.Padding}" Name="HeaderSite" MinWidth="0" MinHeight="0" Margin="1,1,1,1" DockPanel.Dock="Top">
            <ToggleButton.Style>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                          </Grid.ColumnDefinitions>
                          <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                          <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="4,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </ToggleButton.Style>
            <ToggleButton.FocusVisualStyle>
              <Style TargetType="IFrameworkInputElement">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate>
                      <Border>
                        <Rectangle Stroke="#FF000000" StrokeThickness="1" StrokeDashArray="1 2" Margin="0,0,0,0" SnapsToDevicePixels="True" />
                      </Border>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </ToggleButton.FocusVisualStyle>
          </ToggleButton>
          <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Name="ExpandSite" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" Visibility="Collapsed" Focusable="False" DockPanel.Dock="Bottom" />
        </DockPanel>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="Expander.IsExpanded">
          <Setter TargetName="ExpandSite" Property="UIElement.Visibility" Value="{x:Static Visibility.Visible}" />
          <Trigger.Value>
            <s:Boolean>True</s:Boolean>
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Right}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Left}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.RowDefinitions>
                            <RowDefinition Height="19" />
                            <RowDefinition Height="*" />
                          </Grid.RowDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="-90" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="0,4,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Right" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Top}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Bottom}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                          </Grid.ColumnDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="180" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="4,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Up" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Left}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Right}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.RowDefinitions>
                            <RowDefinition Height="19" />
                            <RowDefinition Height="*" />
                          </Grid.RowDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="90" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="0,4,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Left" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="UIElement.IsEnabled">
          <Setter Property="TextElement.Foreground">
            <Setter.Value>
              <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <s:Boolean>False</s:Boolean>
          </Trigger.Value>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Expander.Template>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</Expander>

I'm trying to change the colour of an Expander's expand button, and it seems the only way to do that is to change its control template, as there is no property to set that.

So, I've used Show Me The Template to grab the template for the Aero theme. I've then set that template on my Expander, with no other changes. It looks fine, but clicking on the button no longer causes it to expand.

Can anybody explain why this is happening? I expect it is something simple, but I'm still learning WPF and haven't been able to figure it out. Thank you.

Here is the complete code:

<Expander Header="Test">
  <Expander.Template>
    <ControlTemplate TargetType="Expander" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Border BorderThickness="{TemplateBinding Border.BorderThickness}" CornerRadius="3,3,3,3" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
        <DockPanel>
          <ToggleButton IsChecked="False" Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" Foreground="{TemplateBinding TextElement.Foreground}" FontFamily="{TemplateBinding TextElement.FontFamily}" FontSize="{TemplateBinding TextElement.FontSize}" FontStretch="{TemplateBinding TextElement.FontStretch}" FontStyle="{TemplateBinding TextElement.FontStyle}" FontWeight="{TemplateBinding TextElement.FontWeight}" HorizontalContentAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding Control.VerticalContentAlignment}" Padding="{TemplateBinding Control.Padding}" Name="HeaderSite" MinWidth="0" MinHeight="0" Margin="1,1,1,1" DockPanel.Dock="Top">
            <ToggleButton.Style>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                          </Grid.ColumnDefinitions>
                          <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                          <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="4,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </ToggleButton.Style>
            <ToggleButton.FocusVisualStyle>
              <Style TargetType="IFrameworkInputElement">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate>
                      <Border>
                        <Rectangle Stroke="#FF000000" StrokeThickness="1" StrokeDashArray="1 2" Margin="0,0,0,0" SnapsToDevicePixels="True" />
                      </Border>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </ToggleButton.FocusVisualStyle>
          </ToggleButton>
          <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Name="ExpandSite" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" Visibility="Collapsed" Focusable="False" DockPanel.Dock="Bottom" />
        </DockPanel>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="Expander.IsExpanded">
          <Setter TargetName="ExpandSite" Property="UIElement.Visibility" Value="{x:Static Visibility.Visible}" />
          <Trigger.Value>
            <s:Boolean>True</s:Boolean>
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Right}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Left}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.RowDefinitions>
                            <RowDefinition Height="19" />
                            <RowDefinition Height="*" />
                          </Grid.RowDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="-90" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="0,4,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Right" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Top}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Bottom}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                          </Grid.ColumnDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="180" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="4,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Up" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="Expander.ExpandDirection">
          <Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="{x:Static Dock.Left}" />
          <Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="{x:Static Dock.Right}" />
          <Setter TargetName="HeaderSite" Property="FrameworkElement.Style">
            <Setter.Value>
              <Style TargetType="ToggleButton">
                <Style.Resources>
                  <ResourceDictionary />
                </Style.Resources>
                <Setter Property="Control.Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                      <Border Padding="{TemplateBinding Control.Padding}">
                        <Grid Background="#00FFFFFF" SnapsToDevicePixels="False">
                          <Grid.RowDefinitions>
                            <RowDefinition Height="19" />
                            <RowDefinition Height="*" />
                          </Grid.RowDefinitions>
                          <Grid>
                            <Grid.LayoutTransform>
                              <TransformGroup>
                                <TransformGroup.Children>
                                  <RotateTransform Angle="90" />
                                </TransformGroup.Children>
                              </TransformGroup>
                            </Grid.LayoutTransform>
                            <Ellipse Stroke="#FFA9A9A9" Name="circle" Width="19" Height="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Path Data="M1,1.5L4.5,5 8,1.5" Stroke="#FF666666" StrokeThickness="2" Name="arrow" HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="False" />
                          </Grid>
                          <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="0,4,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" />
                        </Grid>
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked">
                          <Setter TargetName="arrow" Property="Path.Data">
                            <Setter.Value>
                              <StreamGeometry>M1,4.5L4.5,1 8,4.5</StreamGeometry>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="UIElement.IsMouseOver">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF222222</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed">
                          <Setter TargetName="circle" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF526C7B</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="circle" Property="Shape.StrokeThickness">
                            <Setter.Value>
                              <s:Double>1.5</s:Double>
                            </Setter.Value>
                          </Setter>
                          <Setter TargetName="arrow" Property="Shape.Stroke">
                            <Setter.Value>
                              <SolidColorBrush>#FF003366</SolidColorBrush>
                            </Setter.Value>
                          </Setter>
                          <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                          </Trigger.Value>
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <x:Static Member="ExpandDirection.Left" />
          </Trigger.Value>
        </Trigger>
        <Trigger Property="UIElement.IsEnabled">
          <Setter Property="TextElement.Foreground">
            <Setter.Value>
              <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
            </Setter.Value>
          </Setter>
          <Trigger.Value>
            <s:Boolean>False</s:Boolean>
          </Trigger.Value>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Expander.Template>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</Expander>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-10-18 11:20:28

当我在 Blend 中编辑 Expander 模板的副本时(它应该执行与使用 ShowMeTheTemplate 执行的操作大致相同的操作),IsChecked 属性名为 HeaderSiteToggleButton 的值不是 False (因为它在上面的 XAML 中),而是这个表达式:

IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 

所以按钮的切换状态是绑定到控件的 IsExpanded 属性。这就是切换按钮生效的方式。

When I edit a copy of the Expander template in Blend, which should do roughly the same thing as you are doing with ShowMeTheTemplate, the IsChecked property of the ToggleButton named HeaderSite is not False (as it is in your XAML above) but this expression:

IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 

So the toggle state of the button is bound to the IsExpanded property of the control. That's the way the toggle button takes effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文