WPF VisualState - нажатое событие не срабатывает

Я впервые играю с 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>

person CubemonkeyNYC    schedule 28.01.2017    source источник


Ответы (1)


Я обнаружил, что пустое событие Focused, которое на кнопке запускается только при нажатии, переопределяет событие Pressed. Просто удалив пустое событие Focused, событие Pressed начало работать.

person CubemonkeyNYC    schedule 28.01.2017