【问题标题】:Silverlight DataGrid MouseLeftButtonDown event not raised when clicking on rows单击行时未引发 Silverlight DataGrid MouseLeftButtonDown 事件
【发布时间】:2012-03-02 14:55:06
【问题描述】:

注意:我找到了解决问题的方法,因此我将其发布以供参考,尽管我很乐意接受更好的解决方案的教育。

我正在尝试通过挂钩 UIElement.MouseLeftButtonDown 在 Silverlight DataGrid 上提供 双击 功能,但是当我使用 XAML 或 DataGrid.MouseLeftButtonDown += 语法订阅 DataGrid.MouseLeftButtonDown 时,我的事件当我单击 DataGrid 中的行时,不会调用处理程序。如果我单击标题,则会引发事件。

如果我在父 UserControl 级别订阅相同的事件,则事件处理程序将根据Silverlight RoutedEvents 的预期成功调用,但随后我必须检测点击是否发生在DataGrid 或其他地方。

如果我使用UIElement.AddHandler 语法订阅事件,如下所示,那么它会根据handledEventsToo: true 参数按预期工作。

dataGrid.AddHandler(UIElement.MouseLeftButtonDownEvent, 
                    new MouseButtonEventHandler(dataGrid_MouseLeftButtonDown)
                    , handledEventsToo: true);

DataGrid 实现似乎将这些事件标记为已处理,以防止事件冒泡,默认情况下在其中一个子 UIElement 中,这不是我最初所期望的。经过更多思考,我可以看到点击行为会驱动各种事情(选择项目、编辑字段等),所以也许实现是有意义的。

【问题讨论】:

    标签: silverlight silverlight-4.0 datagrid event-handling


    【解决方案1】:

    我遇到了同样的问题,我使用了触发事件的 MouseLeftButtonUp,但点击计数值始终为 1。

    解决方法如下:

    private const int MOUSE_SENSITIVITY = 300;
    
    private DateTime _previousClick;
    
    private void exceptionsDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
            DataGrid dg = (sender as DataGrid);
            DateTime current=DateTime.Now;
            LoggerService.Exception exception = (LoggerService.Exception)dg.SelectedItem;
            if (_previousClick != null)
            {
                TimeSpan clickSpan = current - _previousClick;
                if (clickSpan.TotalMilliseconds < MOUSE_SENSITIVITY)
                {
                    MessageBox.Show("You double clicked!");
                }
            }
            _previousClick = current;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2011-12-22
      相关资源
      最近更新 更多