WPF交互触发器MouseDoubleClick事件未触发

时间:2015-09-28 10:29:14

标签: c# wpf double mouseevent eventtrigger

我在以下.xaml文件中有一个MouseDoubleClick事件,该文件未正确触发:

<Style TargetType="controls:Source" x:Key="{x:Type controls:Source}">
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="controls:Source">
      <Border BorderThickness="0.8" CornerRadius="10" BorderBrush="{TemplateBinding Foreground}"
              d:DataContext="{d:DesignInstance d:Type=viewModels:SourceViewModel}">
        <Grid Style="{StaticResource ElementThatClipsToParentBordersStyle}">
          <Image Source="{Binding AttachedSource.Image, RelativeSource={RelativeSource TemplatedParent}}" Stretch="Fill" />

          <Border x:Name="Selection"
                  Opacity="0.3"
                  Background="{TemplateBinding Foreground}"
                  Visibility="Collapsed"
                  IsHitTestVisible="False"/>
        </Grid>

        <i:Interaction.Triggers>
          <i:EventTrigger EventName="MouseLeftButtonDown">
            <interactivity:InvokeCommandAction Command="{Binding DataContext.SelectionState.ClickSourceCommand, ElementName=SourcesControl}"
                                               CommandParameter="{Binding}" />
          </i:EventTrigger>

          <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
            <interactivity:InvokeCommandAction Command="{Binding PreviewLeftClickCommand}" />
          </i:EventTrigger>

          <i:EventTrigger EventName="MouseMove">
            <interactivity:InvokeCommandAction Command="{Binding MouseMoveCommand}" />
          </i:EventTrigger>

          <i:EventTrigger EventName="MouseDoubleClick">
            <interactivity:InvokeCommandAction Command="{Binding SourceDoubleClickCommand}"
                                               CommandParameter="{Binding}" />
          </i:EventTrigger>
        </i:Interaction.Triggers>

      </Border>

      <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter TargetName="Selection" Property="Visibility" Value="Visible" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Setter.Value>
</Setter>

每个其他事件都可以正常运行,但不会执行绑定到MouseDoubleClick事件的命令。

奇怪的是,当我使用其他事件(如MouseLeftButtonDown)时,一切正常,因此问题似乎与被执行的命令无关,而是与已使用的事件本身有关。

当然,我可以通过使用一个简单的MouseLeftButtonDown事件来解决这个问题,并检查最后一次点击的时间以识别双击,但内置的双击事件似乎是更适合我的方式。

有人知道为什么MouseDoubleClick事件在这种情况下无法正常启动吗?

1 个答案:

答案 0 :(得分:1)

Border没有MouseDoubleClick事件,因为它不是控件。 有两种方法可以使MouseDoubleClick可用。请参阅hash block constructor

一种直截了当的方法是将您的边框包裹在ContentControl中,从而引发MouseDoubleClick事件(来自Why doesn't WPF border control have a mousedoubleclick event?):

<ContentControl MouseDoubleClick="OnDoubleClick">
  <Border Margin="10" BorderBrush="Black" BorderThickness="2">
    <Grid Margin="4">
        <Rectangle Fill="Red" />
        <TextBlock Text="Hello" FontSize="15" />
    </Grid>
  </Border>
</ContentControl>