Xaml:MouseLeftButtonUp事件无法正常工作

时间:2014-02-27 13:23:08

标签: c# .net wpf visual-studio xaml

我有一个声明为ControlTemplate的Button的xaml代码 App.xaml

 <!--Style for button start here-->
  <Style  x:Key="myButtonStyle" TargetType="Border">
  <EventSetter Event="Border.MouseLeftButtonDown" Handler="ButtonDown" />
    <EventSetter Event="Border.MouseLeftButtonUp" Handler="ButtonUp" />
    <EventSetter Event="Border.MouseEnter" Handler="ButtonEnter" />
    <EventSetter Event="Border.MouseLeave" Handler="ButtonLeave"/>

</Style>

<!--Style for myButtonStyle ends here-->

<!--Control Template for button start here-->
<ControlTemplate TargetType="Button" x:Key="buttonPrimary">
    <Grid Width="{TemplateBinding Width}">
        <Border Height="35" Width="{TemplateBinding Width}" Style="{StaticResource myButtonStyle}"  Background="{TemplateBinding Background}" CornerRadius="3" Loaded="borderLoaded"  x:Name="myButton">
                <ContentPresenter  HorizontalAlignment="Center"  VerticalAlignment="Center" >
                    <TextBlock.Foreground>
                        White
                    </TextBlock.Foreground>
                    <TextBlock.FontSize>14</TextBlock.FontSize>

                </ContentPresenter>
        </Border>
    </Grid>

</ControlTemplate> 

在另一个文件中我处理了所有鼠标事件.. Main.cs

 private void ButtonDown(object sender, RoutedEventArgs e)
   {
       Console.WriteLine("ButtonDown");

   }//Button Down method ends here


   private void ButtonUp(object sender, RoutedEventArgs e)
   {
       Console.WriteLine("Button Up");

   }//ButtonUp method ends here


   private void ButtonLeave(object sender, RoutedEventArgs e)
   {
       Console.WriteLine("ButtonLeave");
   }


   private void ButtonEnter(object sender, RoutedEventArgs e)
   {
       Console.WriteLine("ButtonEnter");
   }

在main.xaml中我正在调用这些按钮.. Main.xaml

 <Button   Content="ButtonPrimary" Width="110" Background="#428bca" Template="{StaticResource buttonPrimary}"  />

除了MouseLeftButtonUp .. ??

之外,所有事件都被触发

1 个答案:

答案 0 :(得分:3)

Button的click事件会占用MouseUp事件。

所以:

    <Style x:Key="buttonPrimary" TargetType="Button">
        <EventSetter Event="Click" Handler="ButtonBase_OnClick"/>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ButtonDown" />
        <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ButtonUp" />
        <EventSetter Event="MouseEnter" Handler="ButtonEnter" />
        <EventSetter Event="MouseLeave" Handler="ButtonLeave"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Width="{TemplateBinding Width}">
                        <Border Height="35" Width="{TemplateBinding Width}" Background="{TemplateBinding Background}" CornerRadius="3"   x:Name="myButton">
                        <ContentPresenter  HorizontalAlignment="Center"  VerticalAlignment="Center" >
                            <TextBlock.Foreground>
                                White
                            </TextBlock.Foreground>
                            <TextBlock.FontSize>14</TextBlock.FontSize>

                        </ContentPresenter>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>          
    </Style>

然后添加刚刚处理click事件的ButtonBase_OnClick事件:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e){
    e.Handled = true;
}

然后将您的按钮从Template =更改为Style =然后您将获得所有活动。