Posted By

mkurdziel on 05/24/10


Tagged


Versions (?)

Listbox drag scroll


 / Published in: C#
 

URL: http://stackoverflow.com/questions/1316251/wpf-listbox-auto-scroll-while-dragging

  1. private void ItemsList_DragOver(object sender, System.Windows.DragEventArgs e)
  2. {
  3. ListBox li = sender as ListBox;
  4. ScrollViewer sv = FindVisualChild<ScrollViewer>(ItemsList);
  5.  
  6. double tolerance = 10;
  7. double verticalPos = e.GetPosition(li).Y;
  8. double offset = 3;
  9.  
  10. if (verticalPos < tolerance) // Top of visible list?
  11. {
  12. sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up.
  13. }
  14. else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list?
  15. {
  16. sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down.
  17. }
  18. }
  19.  
  20. public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  21. {
  22. // Search immediate children first (breadth-first)
  23. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  24. {
  25. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  26.  
  27. if (child != null && child is childItem)
  28. return (childItem)child;
  29.  
  30. else
  31. {
  32. childItem childOfChild = FindVisualChild<childItem>(child);
  33.  
  34. if (childOfChild != null)
  35. return childOfChild;
  36. }
  37. }
  38.  
  39. return null;
  40. }

Report this snippet  

You need to login to post a comment.