c# - Silverlight 5 双击总是将 ClickCount 返回为 1

标签 c# .net silverlight

我为 DataGrid 创建了检测双击的行为:

public class DataGridDoubleClickBehavior : Behavior<DataGrid>    
    {
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
            "CommandParameter",
            typeof(object),
            typeof(DataGridDoubleClickBehavior),
            new PropertyMetadata(null));        

        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }            
            set { SetValue(CommandParameterProperty, value); }
        }

        public static readonly DependencyProperty DoubleClickCommandProperty = DependencyProperty.Register(
            "DoubleClickCommand",
            typeof(ICommand),
            typeof(DataGridDoubleClickBehavior),
            new PropertyMetadata(null));       

        public ICommand DoubleClickCommand
        {
            get { return (ICommand)GetValue(DoubleClickCommandProperty); }            
            set { SetValue(DoubleClickCommandProperty, value); }
        }

        protected override void OnAttached()
        {
            this.AssociatedObject.LoadingRow += this.OnLoadingRow;
            this.AssociatedObject.UnloadingRow += this.OnUnloadingRow;

            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.LoadingRow -= this.OnLoadingRow;
            this.AssociatedObject.UnloadingRow -= this.OnUnloadingRow;

            base.OnDetaching();
        } 

        private void OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += this.OnMouseLeftButtonUp;
        }

        private void OnUnloadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp -= this.OnMouseLeftButtonUp;
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount < 2) return;

            if (this.DoubleClickCommand != null) this.DoubleClickCommand.Execute(this.CommandParameter);
        }
    }

一切似乎都很好,只是它没有记录多次点击。在 OnMouseLeftButtonUp 中,ClickCount 始终为 1。有人知道为什么吗?

最佳答案

我找到了一个非常简单的解决方案。只需替换事件处理程序注册语法

myDataGrid.MouseLeftButtonDown += this.MyDataGrid_MouseLeftButtonDown;

使用 AddHandler 语法

myDataGrid.AddHandler(DataGrid.MouseLeftButtonDownEvent,
    new MouseButtonEventHandler(this.MyDataGrid_MouseLeftButtonDown),
    handledEventsToo: true)

这样可以指定神奇的 bool 值 handledEventsToo 参数。

这也将处理已处理的事件。

关于c# - Silverlight 5 双击总是将 ClickCount 返回为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764837/

相关文章:

c# - WaitAll 和完成事件的线程问题 - 信号触发异常

c# - .NET 框架中的线程生命周期

c# - 如何处理(国家)代码列表中的弃用值

c# - 为什么我的时间增加了两个小时

silverlight - 更新正在积极使用的 Silverlight 部署的最佳实践

c# - 如何(我应该)模拟 DocumentClient 进行 DocumentDb 单元测试?

c# - MasterPage 的 UserControl 中的 ScriptManager.RegisterClientScriptBlock

c# - 编辑列表中的项目<T>

.net - 银光;好,坏还是丑?

c# - Silverlight 中动态加载的程序集调用锁定文件的 DllImprot