拖动时WPF列表框自动滚动

我有一个具有ListBox的WPF应用程序。 拖动机制已经实现,但是当列表太长并且我想将项目移动到不可见的位置时我不能。

例如,屏幕显示10个项目。 我有20件物品。 如果我想将最后一项拖到第一个位置,我必须拖到顶部并放下。 向上滚动并再次拖动。

如何使ListBox自动滚动?

得到它了。 使用ListBox的事件DragOver ,使用此处找到的函数获取列表框的scrollviewer ,之后它只是稍微与位置scrollviewer

 private void ItemsList_DragOver(object sender, System.Windows.DragEventArgs e) { ListBox li = sender as ListBox; ScrollViewer sv = FindVisualChild(ItemsList); double tolerance = 10; double verticalPos = e.GetPosition(li).Y; double offset = 3; if (verticalPos < tolerance) // Top of visible list? { sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up. } else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list? { sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down. } } public static childItem FindVisualChild(DependencyObject obj) where childItem : DependencyObject { // Search immediate children first (breadth-first) for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild(child); if (childOfChild != null) return childOfChild; } } return null; } 

基于此,我创建了一个可以像这样轻松使用的附加行为 –

  

这是附加行为的代码 -

 ///  /// Provides extended support for drag drop operation ///  public static class DragDropExtension { public static read-only DependencyProperty ScrollOnDragDropProperty = DependencyProperty.RegisterAttached("ScrollOnDragDrop", typeof(bool), typeof(DragDropExtension), new PropertyMetadata(false, HandleScrollOnDragDropChanged)); public static bool GetScrollOnDragDrop(DependencyObject element) { if (element == null) { throw new ArgumentNullException("element"); } return (bool)element.GetValue(ScrollOnDragDropProperty); } public static void SetScrollOnDragDrop(DependencyObject element, bool value) { if (element == null) { throw new ArgumentNullException("element"); } element.SetValue(ScrollOnDragDropProperty, value); } private static void HandleScrollOnDragDropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { FrameworkElement container = d as FrameworkElement; if (d == null) { Debug.Fail("Invalid type!"); return; } Unsubscribe(container); if (true.Equals(e.NewValue)) { Subscribe(container); } } private static void Subscribe(FrameworkElement container) { container.PreviewDragOver += OnContainerPreviewDragOver; } private static void OnContainerPreviewDragOver(object sender, DragEventArgs e) { FrameworkElement container = sender as FrameworkElement; if (container == null) { return; } ScrollViewer scrollViewer = GetFirstVisualChild(container); if (scrollViewer == null) { return; } double tolerance = 60; double verticalPos = e.GetPosition(container).Y; double offset = 20; if (verticalPos < tolerance) // Top of visible list? { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset); //Scroll up. } else if (verticalPos > container.ActualHeight - tolerance) //Bottom of visible list? { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset); //Scroll down. } } private static void Unsubscribe(FrameworkElement container) { container.PreviewDragOver -= OnContainerPreviewDragOver; } public static T GetFirstVisualChild(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { return (T)child; } T childItem = GetFirstVisualChild(child); if (childItem != null) { return childItem; } } } return null; } }