2015-09-28 197 views
0

我有以下的.xaml文件MouseDoubleClick事件未正确触发:WPF互动触发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,但内置的doubleclick事件似乎是更好的方式。

有人有一个想法,为什么在这种情况下MouseDoubleClick事件没有正确触发?

回答

0

Border没有MouseDoubleClick事件,因为它不是控件。 有几种方法使MouseDoubleClick可用。见Why doesn't WPF border control have a mousedoubleclick event?

直接的方法来包装你的边界在ContentControl这提高了MouseDoubleClick事件(从http://blogs.microsoft.co.il/pavely/2011/08/03/wpf-tip-using-a-mousedoubleclick-event-where-none-exists/):

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

感谢您的快速回复,添加内容控制工作就像一个魅力! 有没有什么办法可以实现边界不支持MouseDoubleClick事件,除了“手动”寻找边界的支持事件? Visual Studio本身并没有给我任何提示...... – Kerl

+0

我想你可以在设计器窗口中看到事件。就我而言,我通过搜索诸如“wpf border click-event”之类的东西来了解它,并且我可能仍然会先尝试。一个问题是意识到问题出在边界而不是你的代码上。 – PScr