Wednesday, 7 July 2010

LISTBOX DRAG SCROLL

  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. double tolerance = 10;
  6. double verticalPos = e.GetPosition(li).Y;
  7. double offset = 3;
  8. if (verticalPos < tolerance) // Top of visible list?
  9. {
  10. sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up.
  11. }
  12. else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list?
  13. {
  14. sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down.
  15. }
  16. }
  17. public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  18. {
  19. // Search immediate children first (breadth-first)
  20. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  21. {
  22. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  23. if (child != null && child is childItem)
  24. return (childItem)child;
  25. else
  26. {
  27. childItem childOfChild = FindVisualChild<childItem>(child);
  28. if (childOfChild != null)
  29. return childOfChild;
  30. }
  31. }
  32. return null;
  33. }

No comments:

Post a Comment