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 事件来解决这个问题,并检查最后一次单击的时间以识别双击,但内置的双击事件似乎更适合我。
有人知道为什么 MouseDoubleClick 事件在这种情况下没有正确触发吗?
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>
我在以下 .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 事件在这种情况下没有正确触发吗?
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>