Silverlight 的双击触发器 [英] Double Click trigger for Silverlight

查看:18
本文介绍了Silverlight 的双击触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关: Silverlight 中最干净的单击 + 双击处理?"

在 XAML 中使双击触发某些操作的最简单方法是什么?

What is the simplest way to make a double-click trigger some action in XAML?

我正在尝试做这样的事情,当用户双击 ListBox 项目时关闭弹出窗口:

I'm trying to do something like this, closing a popup when the user double-clicks a ListBox item:

<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.

The problem with this above is that, of course, there is no "DoubleClick" event. So I need to replace the EventTrigger with a double-click trigger.

推荐答案

通过继承 TriggerBase 并将侦听器附加到MouseLeftButtonDown"事件来创建自定义双击触发器.检查MouseButtonEventArgs.ClickCount"属性以检查双击:

Create a custom double-click trigger by inheriting TriggerBase<T> and attaching a listener to the "MouseLeftButtonDown" event. Check the "MouseButtonEventArgs.ClickCount" property to check for the double-click:

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 来工作.

With this, the XAML in the OP should work by replacing the EventTrigger with the above DoubleClickTrigger.

这篇关于Silverlight 的双击触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆