EnhancedScrollViewer.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. namespace MediaBrowser.UI.Controls
  6. {
  7. /// <summary>
  8. /// Provides a ScrollViewer that can be scrolled by dragging the mouse
  9. /// </summary>
  10. public class EnhancedScrollViewer : ScrollViewer
  11. {
  12. private Point _scrollTarget;
  13. private Point _scrollStartPoint;
  14. private Point _scrollStartOffset;
  15. private const int PixelsToMoveToBeConsideredScroll = 5;
  16. protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
  17. {
  18. if (IsMouseOver)
  19. {
  20. // Save starting point, used later when determining how much to scroll.
  21. _scrollStartPoint = e.GetPosition(this);
  22. _scrollStartOffset.X = HorizontalOffset;
  23. _scrollStartOffset.Y = VerticalOffset;
  24. // Update the cursor if can scroll or not.
  25. Cursor = (ExtentWidth > ViewportWidth) ||
  26. (ExtentHeight > ViewportHeight) ?
  27. Cursors.ScrollAll : Cursors.Arrow;
  28. CaptureMouse();
  29. }
  30. base.OnPreviewMouseDown(e);
  31. }
  32. protected override void OnPreviewMouseMove(MouseEventArgs e)
  33. {
  34. if (IsMouseCaptured)
  35. {
  36. Point currentPoint = e.GetPosition(this);
  37. // Determine the new amount to scroll.
  38. var delta = new Point(_scrollStartPoint.X - currentPoint.X, _scrollStartPoint.Y - currentPoint.Y);
  39. if (Math.Abs(delta.X) < PixelsToMoveToBeConsideredScroll &&
  40. Math.Abs(delta.Y) < PixelsToMoveToBeConsideredScroll)
  41. return;
  42. _scrollTarget.X = _scrollStartOffset.X + delta.X;
  43. _scrollTarget.Y = _scrollStartOffset.Y + delta.Y;
  44. // Scroll to the new position.
  45. ScrollToHorizontalOffset(_scrollTarget.X);
  46. ScrollToVerticalOffset(_scrollTarget.Y);
  47. }
  48. base.OnPreviewMouseMove(e);
  49. }
  50. protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
  51. {
  52. if (IsMouseCaptured)
  53. {
  54. Cursor = Cursors.Arrow;
  55. ReleaseMouseCapture();
  56. }
  57. base.OnPreviewMouseUp(e);
  58. }
  59. }
  60. }