开发者

WPF ListView ScrollViewer Double-Click Event

Doing the below will reproduce my problem:

  • New WPF Project
  • Add ListView
  • Name the listview: x:Name="lvList"
  • Add enough ListViewItems to the ListView to fill the list completely so a vertical scroll-bar appears during run-time.
  • Put this code in the lvList.MouseDoubleClick event

Debug.Print("Double-Click happened")

  • Run the application
  • Double-click on the LargeChange area of the scroll-bar (Not the scroll "bar" itself)
  • Notice the Immediate window printing the double-click happened message for the ListView

How do I change this behavior so MouseDoubleClick only happens when the mouse is "over" the ListViewItem开发者_如何学Cs and not when continually clicking the ScrollViewer to scroll down/up in the list?


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);
}

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.


Maybe this helps?

Private Sub LstView_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles LstView.MouseDoubleClick
    Dim source As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement)
    If IsNothing(source) Then Return

    Dim TmplParent As DependencyObject = TryCast(source.TemplatedParent, DependencyObject)
    If IsNothing(TmplParent) Then Return
    If Not TmplParent.GetType.Equals(GetType(System.Windows.Controls.ListViewItem)) Then e.Handled = True
End Sub


I don't have VS handy to test if this works, but have you tried handling the double-click event on the ListViewItems rather than the ListView itself?

<ListView ListViewItem.MouseDoubleClick="lv_MouseDoubleClick" ... />

That should handle the MouseDoubleClick event on any child ListViewItem controls inside the ListView. Let us know if it works!


  <Style TargetType="{x:Type ListViewItem}">           
            <EventSetter Event="MouseDoubleClick" Handler="OnListViewDoubleClick" />
        </Style>

If you apply this style, it works. Just double click on item in the listview will work.

also, you have to remove the double click from the listview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜