WPF ListView控件的ScrollViewer双击事件 [英] WPF ListView ScrollViewer Double-Click Event

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

问题描述

在做下面将重现我的问题:

Doing the below will reproduce my problem:


  • 新的WPF项目

  • 添加的ListView

  • 名称列表视图:X:NAME =lvList

  • 添加足够ListViewItems到ListView填充列表完全所以在运行时会出现一个垂直滚动条。

  • 将这个code在lvList.MouseDoubleClick事件

Debug.Print(双点击事)

Debug.Print("Double-Click happened")


  • 运行应用程序

  • 在滚动条的LargeChange面积(不滚动栏本身)
  • 双击
  • 注意,立即窗口打印双击发生的消息为ListView

我要如何改变这种行为,以便MouseDoubleClick只发生当鼠标移动过的ListViewItems,而不是在不断点击的ScrollViewer向下滚动列表/吗?

How do I change this behavior so MouseDoubleClick only happens when the mouse is "over" the ListViewItems and not when continually clicking the ScrollViewer to scroll down/up in the list?

推荐答案

您不能改变行为,因为MouseDoubleClick处理程序连接到ListView控件,因此它具有只要单击ListView的发生 - 任何地方。你可以做它检测到其中的ListView的元素首先检测双击,并从那里弄清楚它是否是一个ListViewItem的或没有。下面是一个简单的例子(省略错误检查):

You can't change the behaviour, because the MouseDoubleClick handler is attached to the ListView control, so it has to occur whenever the ListView is clicked -- anywhere. What you can do it detect which element of the ListView first detected the double-click, and figure out from there whether it was a ListViewItem or not. Here's a simple example (omitting error checking):

private void lv_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  DependencyObject src = (DependencyObject)(e.OriginalSource);
  while (!(src is Control))
    src = VisualTreeHelper.GetParent(src);
  Debug.WriteLine("*** Double clicked on a " + src.GetType().Name);
}

请注意使用e.OriginalSource的找到实际的元素是双击。这通常是一些真正的低水平像一个矩形或TextBlock的,所以我们用VisualTreeHelper步行到含控制。在我简单的例子,我认为我们打的第一个控件将是ListViewItem的,如果你正在处理包含例如CellTemplates这可能并非如此文本框或复选框。但是你可以很容易地改进测试只寻找ListViewItems - 但在这种情况下,不要忘了还有处理的情况下点击任何的ListViewItem之外,搜索最终打的ListView本身

Note the use of e.OriginalSource to find the actual element that was double-clicked. This will typically be something really low level like a Rectangle or TextBlock, so we use VisualTreeHelper to walk up to the containing control. In my trivial example, I've assumed that the first Control we hit will be the ListViewItem, which may not be the case if you're dealing with CellTemplates that contain e.g. text boxes or check boxes. But you can easily refine the test to look only for ListViewItems -- but in that case don't forget to handle the case there the click is outside any ListViewItem and the search eventually hits the ListView itself.

这篇关于WPF ListView控件的ScrollViewer双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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