MouseDown event won't fire after using Click event on same element

时间:2017-04-06 17:20:39

标签: c# wpf custom-controls

I am creating a new Custom Button everything works fine. I am using Storyboard for animation, and the storyboard contains the MouseEnter, MouseLeave, MouseDoun, MouseUp events. After introducing Click event MouseDown event stops working, followed by MouseUp The XAML code of the CustomButton is .....

<Style TargetType="{x:Type local:FlatButton}">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:FlatButton}">
          <Grid>
            <Rectangle Name="Part_Rectangle" Fill="{Binding Path=Background}"/>
            <Label Name="Part_Label" Foreground="White" FontSize="{Binding FontSize, FallbackValue='9'}" 
                   HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                   Style="{StaticResource StyleGray}"
                   Content="{Binding Path=Caption, RelativeSource={RelativeSource TemplatedParent}}"
                   FontFamily="{Binding Path=FontFamily, RelativeSource={RelativeSource TemplatedParent}}"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

The Style for the label is.....

<Style x:Key="StyleGray" TargetType="Label">
    <Setter Property="Background" Value="#00000000"/>
    <Style.Triggers>
      <EventTrigger RoutedEvent="Mouse.MouseEnter">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)"
                                      To="#26000000" Duration="0:0:0.15"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="Mouse.MouseLeave">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)"
                                      To="#00000000" Duration="0:0:0.15"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="Mouse.MouseDown">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)"
                                      To="#3F000000" Duration="0:0:0.15"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="Mouse.MouseUp">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)"
                                          To="#26000000" Duration="0:0:0.15"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Style.Triggers>
  </Style>

And the Implementation of the button is ...

<local:FlatButton Grid.Column="8" x:Name="CloseButton" Caption="&#xE947;"
                                Height="30" Width="45" FontSize="10"
                                ButtonStyle="{StaticResource StyleRed}"/>

And the Code Behind is ...

void CloseBtn_Click(object sender, RoutedEventArgs e)
{
  Window window = this.TemplatedParent as Window;
  if (window != null)
  {
    window.Close();
  }
}

Plese, tell me what I am doing wrong.

1 个答案:

答案 0 :(得分:0)

我发布此答案是因为我无法发表评论。通过查看代码,我认为问题可能与按钮本身的RountedEvents处理有关。

尝试在按钮ControlTemplate Triggers中运行标签的故事板,而不是使用标签的样式,而是针对按钮事件而不是标签事件。