相关: "Cleanest single click + double click handling in Silverlight?"
在XAML中双击触发某些操作的最简单方法是什么?
我正在尝试这样做,当用户双击ListBox项时关闭弹出窗口:
<Popup x:Name="popup">
<ListBox ItemsSource="{Binding Colors}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Color="{Binding Color}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="DoubleClick">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=popup}"
PropertyName="IsOpen" Value="False" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</Popup>
上述问题是,当然,没有“DoubleClick”事件。所以我需要用双击触发器替换EventTrigger
。
答案 0 :(得分:4)
通过继承TriggerBase<T>
并将侦听器附加到“MouseLeftButtonDown”事件来创建自定义双击触发器。检查“MouseButtonEventArgs.ClickCount”属性以检查双击:
public class DoubleClickTrigger : TriggerBase<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnClick), true);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.RemoveHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnClick));
}
private void OnClick(object sender, MouseButtonEventArgs args)
{
if (args.ClickCount == 1)
return;
if (args.ClickCount == 2)
InvokeActions(null);
}
}
这样,OP中的XAML应该通过用上面的DoubleClickTrigger
替换EventTrigger来工作。