WPF拖动滚动无法正常工作 [英] WPF Drag-to-scroll doesn't work correctly

查看:290
本文介绍了WPF拖动滚动无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在我的应用程序中实现一个拖曳到滚动的功能,并在我的路上有问题。有谁能够帮助我?
我有一个ScrollViewer,里面有一个ItemsControl,在ItemsTemplate里面我有一个UserControl。我想在ItemsControl中拖动该UserControl。当我拖动到ItemsControl的边界时,我希望ScrollViewer向下滚动。

  protected override void OnPreviewMouseMove(System。 Windows.Input.MouseEventArgs e)
{
if(this.IsMouseCaptured)
{
//获取新的鼠标位置。
点mouseDragCurrentPoint = e.GetPosition(this);

if(Math.Abs​​(mouseDragCurrentPoint.Y - this.ActualHeight)< = 50)
{
this._scrollStartOffset.Y + = 5;
_containingScrollViewer.ScrollToVerticalOffset(this._scrollStartOffset.Y);
}
if(mouseDragCurrentPoint.Y< = 50)
{
this._scrollStartOffset.Y - = 5;
_containingScrollViewer.ScrollToVerticalOffset(this._scrollStartOffset.Y);
}
}
base.OnPreviewMouseMove(e);
}

当我开始拖动调用 DragDrop.DoDragDrop )滚动不要发生。但是当我禁用拖动时,ScrollViewer会根据鼠标位置滚动。
也许有没有什么东西,我不会在accont关于拖动和捕获鼠标?
感谢关注。
Garegin

解决方案

当使用DragDrop.DoDragDrop()时,我使用一个处理Me.DragOver事件的Sub在VB中),所以它看起来如下。记住你,我的控件有一个包含在ScrollViewer中的ListBox。

  Private Sub ListBox_Items_DragOver(ByVal sender As System.Object,ByVal e作为System.Windows.DragEventArgs)处理Me.DragOver 
Dim currentMousePoint As Point = e.GetPosition(_containtingScrollViewer)
如果Math.Abs​​(currentMousePoint.Y - _containtingScrollViewer.ActualHeight)< = 50然后
_containtingScrollViewer.ScrollToVerticalOffset(currentMousePoint.Y + 5)
End If
如果currentMousePoint.Y< = 50然后
_containtingScrollViewer.ScrollToVerticalOffset(currentMousePoint.Y - 5)
结束如果
End Sub

这使我能够在拖动项目时滚动。您可以根据需要调整公差以获得更好/更平滑的滚动。


I am tying to realize a drag-to-scroll functionality in my application and have problems on my way. Can anybody help me? I have a ScrollViewer and inside it an ItemsControl and within ItemsTemplate I have a UserControl. I want to drag that UserControl within ItemsControl. I want the ScrollViewer to scroll down, when I am dragging to the boundaries of the ItemsControl.

protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e)
{
    if (this.IsMouseCaptured)
    {
        // Get the new mouse position. 
        Point mouseDragCurrentPoint = e.GetPosition(this);

        if (Math.Abs(mouseDragCurrentPoint.Y - this.ActualHeight) <= 50)
        {
            this._scrollStartOffset.Y += 5;
            _containingScrollViewer.ScrollToVerticalOffset(this._scrollStartOffset.Y);
        }
        if (mouseDragCurrentPoint.Y <= 50)
        {
            this._scrollStartOffset.Y -= 5;
            _containingScrollViewer.ScrollToVerticalOffset(this._scrollStartOffset.Y);
        }
    }
    base.OnPreviewMouseMove(e);
}

When I start dragging by calling DragDrop.DoDragDrop() scrolling don't happens. But when I disable dragging, the ScrollViewer scrolls down dependong on mouse position. Maybe there's something that I don't take into accont about dragging and Capturing the mouse? Thanks for attention. Garegin

解决方案

When using DragDrop.DoDragDrop(), I use a Sub that Handles the Me.DragOver event (in VB) so it looks as follows. Mind you, my control has a ListBox wrapped in a ScrollViewer.

Private Sub ListBox_Items_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs) Handles Me.DragOver
        Dim currentMousePoint As Point = e.GetPosition(_containtingScrollViewer)
        If Math.Abs(currentMousePoint.Y - _containtingScrollViewer.ActualHeight) <= 50 Then
            _containtingScrollViewer.ScrollToVerticalOffset(currentMousePoint.Y + 5)
        End If
        If currentMousePoint.Y <= 50 Then
            _containtingScrollViewer.ScrollToVerticalOffset(currentMousePoint.Y - 5)
        End If
End Sub

This gives me the ability to scroll whilst dragging items. You can tweak the tolerances to get better/smoother scrolling as needed.

这篇关于WPF拖动滚动无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆