C# 滚动到scrollview中的选定树视图项
我有一个卷轴查看器包装一个树状视图 我以编程方式填充treeview(它没有绑定),并将treeview扩展到预定的treeview项。一切正常C# 滚动到scrollview中的选定树视图项,c#,wpf,treeview,scrollviewer,C#,Wpf,Treeview,Scrollviewer,我有一个卷轴查看器包装一个树状视图 我以编程方式填充treeview(它没有绑定),并将treeview扩展到预定的treeview项。一切正常 我的问题是,当树展开时,我希望作为树视图父对象的scrollview滚动到我刚才展开的树视图项。有什么想法吗请记住,treeview在每次展开时可能没有相同的结构,因此排除了仅存储当前滚动位置并重置为该位置的可能性…我也遇到了同样的问题,treeview没有滚动到所选项目 我所做的是,在将树扩展到所选TreeViewItem后,调用Dispatcher
我的问题是,当树展开时,我希望作为树视图父对象的scrollview滚动到我刚才展开的树视图项。有什么想法吗请记住,treeview在每次展开时可能没有相同的结构,因此排除了仅存储当前滚动位置并重置为该位置的可能性…我也遇到了同样的问题,treeview没有滚动到所选项目 我所做的是,在将树扩展到所选TreeViewItem后,调用Dispatcher Helper方法以允许UI更新,然后在所选项目上使用TransformToAncestor来查找其在ScrollViewer中的位置。代码如下:
// Allow UI Rendering to Refresh
DispatcherHelper.WaitForPriority();
// Scroll to selected Item
TreeViewItem tvi = myTreeView.SelectedItem as TreeViewItem;
Point offset = tvi.TransformToAncestor(myScroll).Transform(new Point(0, 0));
myScroll.ScrollToVerticalOffset(offset.Y);
以下是DispatcherHelper代码:
public class DispatcherHelper
{
private static readonly DispatcherOperationCallback exitFrameCallback = ExitFrame;
/// <summary>
/// Processes all UI messages currently in the message queue.
/// </summary>
public static void WaitForPriority()
{
// Create new nested message pump.
DispatcherFrame nestedFrame = new DispatcherFrame();
// Dispatch a callback to the current message queue, when getting called,
// this callback will end the nested message loop.
// The priority of this callback should be lower than that of event message you want to process.
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.ApplicationIdle, exitFrameCallback, nestedFrame);
// pump the nested message loop, the nested message loop will immediately
// process the messages left inside the message queue.
Dispatcher.PushFrame(nestedFrame);
// If the "exitFrame" callback is not finished, abort it.
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
private static Object ExitFrame(Object state)
{
DispatcherFrame frame = state as DispatcherFrame;
// Exit the nested message loop.
frame.Continue = false;
return null;
}
}
公共类DispatcherHelper
{
私有静态只读DispatcherOperationCallback exitFrameCallback=ExitFrame;
///
///处理消息队列中当前的所有UI消息。
///
公共静态void WaitForPriority()
{
//创建新的嵌套消息泵。
DispatcherFrame nestedFrame=新DispatcherFrame();
//调用时,向当前消息队列调度回调,
//此回调将结束嵌套的消息循环。
//此回调的优先级应低于要处理的事件消息的优先级。
DispatcherOperation ExitoOperation=Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.ApplicationIdle、exitFrameCallback、nestedFrame);
//泵送嵌套消息循环时,嵌套消息循环将立即
//处理留在消息队列中的消息。
调度程序.推帧(嵌套帧);
//如果“exitFrame”回调未完成,请中止它。
if(ExitoOperation.Status!=DispatcherOperationStatus.Completed)
{
exitoOperation.Abort();
}
}
私有静态对象ExitFrame(对象状态)
{
DispatcherFrame=状态为DispatcherFrame;
//退出嵌套的消息循环。
frame.Continue=false;
返回null;
}
}
Jason的ScrollViewer技巧是将TreeViewItem移动到特定位置的绝佳方法
但有一个问题:在MVVM中,您无法访问视图模型中的ScrollViewer。不管怎么说,这里有一个办法。如果您有一个TreeView项目,您可以沿着它的可视树一直走到嵌入的ScrollViewer:
// Get the TreeView's ScrollViewer
DependencyObject parent = VisualTreeHelper.GetParent(selectedTreeViewItem);
while (parent != null && !(parent is ScrollViewer))
{
parent = VisualTreeHelper.GetParent(parent);
}
谢谢这很有效。应该知道这些项目首先需要“可视化”!看起来offet.Y是相对的,所以myScroll.ScrollToVerticalOffset(offset.Y);需要更改为sv.ScrollToVerticalOffset(偏移量Y+sv.VerticalOffset);