问题描述
好吧,伙计们,我一直在为这个问题抓狂,花了好几个小时试图研究它是如何工作的,但我还没有找到答案,如果你想看看我的任何 SRC,请随意问一下,我看看能不能帮上忙.
Okay guys, I have been scratching my head like mad over this issue and have spent a good few hours trying to research how it works but I am yet to find an answer, if you wish to see any of my SRC feel free to ask about it and I will see if I can help.
基本上我遇到的问题是我的应用程序中有一个 TreeView
文件夹,即:
Basically the issue I am having is that I have a TreeView
of folders in my application i.e:
Catalog
Brands
Nike
Adidas
Lactose
Styles
Sandles
Trainers
Boots
我试图解决的问题是,当我拖动文件夹时(这是在我的 DragDropManager
类中处理的),我无法向上或向下滚动(只是显示一个可爱的停止符号).我也无法在树视图中实际找到滚动条,所以我不确定它是如何生成的(这不是我自己的软件,我最近开始为一家公司工作,所以我不熟悉代码,没有其他人好像知道.)
The issue that I am trying to fix is that when I drag a folder around (This is handled in my DragDropManager
class), I am unable to scroll up or down(simply displays a lovely stop sign). I am also unable to find a scroller actually within the treeview, so I am unsure how it is being generated (This is not my own software, I have recently started working for a company so I am not familiar with the code and no one else seems to know.)
如果我想将某些东西从最顶部移到最底部,这是一个问题.
This is a problem if I want to move something from the very top to the very bottom.
滚动本身可以正常工作,无需拖动.
The scrolling works fine on its own without the dragging being done.
如果有人希望看到我的代码的任何部分,请随时提问,因为我不确定要向你们展示什么.
If anyone wishes to see any part of my code feel free to ask as I am unsure what to actually show you guys.
我已经阅读了几篇不错的文章,但只是摸不着头脑.
I have read through a good few articles and am just left scratching my head.
推荐答案
我已经创建了一个附加属性来实现此行为,请查看我的帖子 -
I have created an attached property for achieving this behavior, have a look at my post here -
主要逻辑是这样的-
private static void OnContainerPreviewDragOver(object sender, DragEventArgs e)
{
FrameworkElement container = sender as FrameworkElement;
if (container == null) { return; }
ScrollViewer scrollViewer = GetFirstVisualChild<ScrollViewer>(container);
if (scrollViewer == null) { return; }
double tolerance = 60;
double verticalPos = e.GetPosition(container).Y;
double offset = 20;
if (verticalPos < tolerance) // Top of visible list?
{
//Scroll up
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset);
}
else if (verticalPos > container.ActualHeight - tolerance) //Bottom of visible list?
{
//Scroll down
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset);
}
}
关于 SO 的类似问题(虽然它们主要针对 ListBox
/ListView
但也适用于 TreeView
)-
Similar questions on SO (although they are mostly for ListBox
/ListView
but should work for TreeView
too) -
这篇关于拖放时滚动 (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!