The method assigned to the MouseLeftButtonDown event handler is not called.

// App

<Style x:Key="SeriousStyle" TargetType="{x:Type Button}" x:Name="AAA"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown" > <BeginStoryboard> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" Duration="0:0:0.6" RepeatBehavior="0:0:3"> <DiscreteColorKeyFrame Value="Lime" KeyTime="00:00:0" /> <DiscreteColorKeyFrame Value="Red" KeyTime="00:00:0.3" /> </ColorAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Style.Triggers> </Style> 

MainWindow.xaml

 <Button x:Name="btnFirst" Content="Bla" HorizontalAlignment="Left" Margin="27,189,0,0" VerticalAlignment="Top" Width="223" Height="79" MouseLeftButtonDown="AnswerWasChosen" Style="{DynamicResource SeriousStyle }" > </Button> 
  • And without the style, the problem cannot be reproduced? - VladD
  • without style everything works, like - Michael Antonkin
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Use either the PreviewMouseLeftButtonDown event, or Click.

It is quite possible that you don’t need a button at all, you can do everything that I saw in the style for another simpler element, like ContentControl:

App.xaml

  <Style x:Key="SeriousStyle" TargetType="{x:Type ContentControl}"> <Setter Property="Background" Value="{x:Static Brushes.Transparent}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ContentControl}"> <Border Background="{TemplateBinding Background}" BorderBrush="#FF9E9E9E" BorderThickness="1" CornerRadius="1"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <EventTrigger RoutedEvent="ContentControl.PreviewMouseLeftButtonDown"> <BeginStoryboard> <Storyboard> <ColorAnimationUsingKeyFrames Duration="0:0:0.6" RepeatBehavior="0:0:3" Storyboard.TargetProperty="(ContentControl.Background).(SolidColorBrush.Color)"> <DiscreteColorKeyFrame KeyTime="00:00:0" Value="Lime" /> <DiscreteColorKeyFrame KeyTime="00:00:0.3" Value="Red" /> </ColorAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Style.Triggers> </Style> 

MainWindow.xaml

  <ContentControl x:Name="btnFirst" Width="223" Height="79" Margin="27,189,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{Binding Counter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" MouseLeftButtonDown="AnswerWasChosen" Style="{DynamicResource SeriousStyle}" /> 

You can read about your problem in MSDN .

    Addition to the previous answer.

    From the documentation :

    The OnMouseLeftButtonDown method marks the OnMouseLeftButtonDown event as handled. To respond to the MouseLeftButtonDown event, attach an event handler to the PreviewMouseLeftButtonDown event or call the AddHandler(RoutedEvent, Delegate, Boolean) method AddHandler(RoutedEvent, Delegate, Boolean) , setting the parameter handledEventsToo to true .

    Thus, the event should not be delivered, regardless of style. Use the workarounds specified in the documentation.

    • But he already uses the PreviewMouseLeftButtonDown event, something I don’t understand both answers - ixSci
    • @ixSci: As I understand it, the vehicle complains that it does not work out AnswerWasChosen . The style is essentially not involved in the problem. - VladD
    • understood, I just looked at the style and stubbornly did not see another piece of code. - ixSci