繁体   English   中英

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

[英]WPF Interaction Trigger MouseDoubleClick event not firing

我在以下.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事件来解决此问题,并检查最后一次单击的时间以识别一个双击,但是内置的doubleclick事件似乎是更好的选择。

有人知道为什么在这种情况下MouseDoubleClick事件不能正确触发吗?

Border没有MouseDoubleClick事件,因为它不是控件。 有两种方法可以使MouseDoubleClick可用。 请参见为什么WPF边框控件没有mousedoubleclick事件?

一种直接的方法是将边框包裹在引发MouseDoubleClick事件的ContentControl中(来自http://blogs.microsoft.co.il/pavely/2011/08/03/wpf-tip-using-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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM