(VB.NET + WPF) 拖动 +在滚动查看器中删除(以允许用户排序)堆栈面板元素?
我有一个很好的模型(我认为!),用于如何允许用户拖动堆栈面板中的元素并将其重新定位到堆栈面板内的另一个位置。
但是,我的 Stackpanel 放置在 ScrollViewer 中,如下所示(概括):
<ScrollViewer>
<StackPanel>
....First item
....Second item
....Third item
....Etc.
</StackPanel>
<ScrollViewer>
问题是,我希望模拟 Word 等程序的功能,如果我将选定的内容(或对象)拖动到可视区域之外,窗口将沿鼠标方向滚动,以查看更多放置我的漂亮小物体的地方。
...即,如果我在拖动堆栈面板的内容时将鼠标移动到 ScrollViewer 的顶部,我希望滚动查看器缓慢向上移动,以便我可以看到更多放置内容的位置。
有什么建议吗?
如果你能帮我解决这个问题,那你就是天赐之物!
I have good model (I think!) for how to allow a user to drag an element in a stackpanel and reposition it to another location within the stackpanel.
However, my Stackpanel is placed within a ScrollViewer, like this (generalized):
<ScrollViewer>
<StackPanel>
....First item
....Second item
....Third item
....Etc.
</StackPanel>
<ScrollViewer>
Here is the problem, I wish to simulate the functionality of programs like word, where if I am dragging selected content (or an object) outside the viewable area, the window will scroll in the direction of the mouse to see more places to drop my nifty little object.
...i.e. If I move the mouse to the top of my ScrollViewer while dragging a stackpanel's contents, I want the scrollviewer to slowly move up so I can see more locations to drop my content.
Any suggestions?
If you can help me solve this, you will be a godsend!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没问题。在
ScrollViewer
级别处理DragOver
路由事件。获得位置。如果它靠近 ScrollViewer 边界的顶部,请向上滚动。如果它靠近 ScrollViewer 边界的底部,请向下滚动。滚动本身是通过调用
scrollViewer.LineUp()
或scrollViewer.LineDown()
完成的。DragOver
事件频繁发生,因此每次调用LineUp()
或LineDown 时,请将
。在再次调用它们之前,请检查DateTime.Now
的值保存在字段中()DateTime.Now
,如果没有足够的时间,则不要调用LineUp()
或LineDown()
。为了更好地控制滚动速度,您可以使用
scrollViewer.ScrollToVerticalOffset(scrollViewer.ContentVerticalOffset + delta)
代替scrollViewer.LineUp()
和scrollViewer.LineDown()< /代码>。
如果您允许在靠近滚动查看器顶部或底部时更快地滚动,则可以提供更好的用户体验。这可以通过将滚动区域划分为多个区域或根据鼠标位置计算速度来完成。在这种情况下,可以通过在靠近边缘时多次调用
LineUp()/LineDown()
来完成速度更改,或者通过增加delta
值(如果您使用的是ScrollToVerticalOffset
。您可能不应该为此目的修改计时(DateTime.Now 比较),因为它不可靠。No problem. Handle the
DragOver
routed event at theScrollViewer
level. Get the position. If it is near the top of the ScrollViewer bounds, scroll up. If it is near the bottom of the ScrollViewer bounds, scroll down.The scrolling itself is done by calling
scrollViewer.LineUp()
orscrollViewer.LineDown()
.The
DragOver
events come frequently, so save the value ofDateTime.Now
in a field each time you callLineUp()
orLineDown()
. Before calling them again, checkDateTime.Now
and if not enough time has elapsed, don't callLineUp()
orLineDown()
.For better control over scrolling speed you can use
scrollViewer.ScrollToVerticalOffset(scrollViewer.ContentVerticalOffset + delta)
instead ofscrollViewer.LineUp()
andscrollViewer.LineDown()
.You can provide a better user experience if you allow faster scrolling when nearer to the top or bottom of the scroll viewer. This can be done by dividing the scroll area into zones, or calculating the speed from the mouse position. In this case, speed changes can be done by calling
LineUp()/LineDown()
multiple times when closer to the edge, or by increasing thedelta
value if you are usingScrollToVerticalOffset
. You probably should not modify the timing (DateTime.Now comparison) for this purpose, because it will be unreliable.