【问题标题】:WPF VisualState - "Pressed" event will not fireWPF VisualState - “按下”事件不会触发
【发布时间】:2017-01-28 14:57:02
【问题描述】:

我是第一次使用 XAML ControlTemplates,并且在我正在处理的一个基本动画上度过了一段奇怪的时光。

在下面的样式中,Pressed 视觉状态绝对不会触发 ScaleTransform。如果我将 Pressed 更改为 Focused 或 MouseOver,它就可以正常工作。

我错过了什么?

      <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
          <Setter Property="VerticalContentAlignment" Value="Center" />
          <Setter Property="HorizontalContentAlignment" Value="Center" />
          <Setter Property="SnapsToDevicePixels" Value="True" />
          <Setter Property="BorderThickness" Value="1" />
          <Setter Property="BorderBrush" Value="{StaticResource Button.BorderBrush}" />
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
                      <Border x:Name="Button_Border"
                              Background="{StaticResource Button.BackgroundBrush}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              Padding="5,2,5,2"
                              CornerRadius="2">
                          <Border.RenderTransform>
                              <ScaleTransform x:Name="Button_ScaleTransform"
                                              CenterX="0.5"
                                              CenterY="0.5" />
                          </Border.RenderTransform>
                          <VisualStateManager.VisualStateGroups>
                              <VisualStateGroup>
                                  <VisualState x:Name="Normal" />


                                  <VisualState x:Name="Pressed">
                                      <Storyboard>
                                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Button_Border"
                                                                         Storyboard.TargetProperty="RenderTransform.ScaleX">
                                              <EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="0.9" />
                                          </DoubleAnimationUsingKeyFrames>
                                          <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Button_Border"
                                                                         Storyboard.TargetProperty="RenderTransform.ScaleY">
                                              <EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="0.9" />
                                          </DoubleAnimationUsingKeyFrames>
                                      </Storyboard>
                                  </VisualState>


                                  <VisualState x:Name="MouseOver" />
                                  <VisualState x:Name="Focused" />
                              </VisualStateGroup>
                          </VisualStateManager.VisualStateGroups>
                          <ContentPresenter />
                      </Border>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>

【问题讨论】:

    标签: c# wpf xaml visualstatemanager


    【解决方案1】:

    我发现只有在单击按钮时才会触发按钮上的空 Focused 事件,它覆盖了 Pressed 事件。通过简单地删除空的 Focused 事件,Pressed 事件开始工作。

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      相关资源
      最近更新 更多