How to detect double click on list view scroll bar?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 02:04:04

I tested the above code which was very helpful, but found the following to be more stable, as sometimes the source gets reported as GridViewRowPresenter when in fact you are double clicking an item.

var src = VisualTreeHelper.GetParent((DependencyObject)e.OriginalSource);
var srcType = src.GetType();
if (srcType == typeof(ListViewItem) || srcType == typeof(GridViewRowPresenter))
{
    // Your logic here
}
Ioannis Stavrinides

Try this in you MouseDoubleClick event on the first Listview:

DependencyObject src = VisualTreeHelper.GetParent((DependencyObject)e.OriginalSource);

if(src is Control && src.GetType() == typeof(ListViewItem))
{
    // Your logic here
}

Based on this.

I am using this in various projects and it solves the problem you are facing.

private void ListBox_OnMouseDoubleClick(object pSender, MouseButtonEventArgs pE)
{
  FrameworkElement originalSource = pE.OriginalSource as FrameworkElement;
  FrameworkElement source = pE.Source as FrameworkElement;

  if (originalSource.DataContext != source.DataContext)
  {
      logic here
  }         
}

When you have the DataContext you can easy see if the sender is an item or the main listbox

I've got the final solution:

private void ListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var originalSource = (DependencyObject)e.OriginalSource;
    while ((originalSource != null) && !(originalSource is ListViewItem)) originalSource = VisualTreeHelper.GetParent(originalSource);
    if (originalSource == null) return;
}

it works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!