TreeView:选择项目时防止水平滚动

问题描述 投票:0回答:1

这是正在发生的事情:

TreeView scrolls horizontally when navigating

期望的结果:

  • 选择任何项目时,不应发生水平滚动(即使该项目太大而无法显示)
  • 解决方案必须与虚拟化配合使用
  • 水平滚动条有效
  • 用户可以使用鼠标或按键水平滚动(如果可用)

有一些残酷的黑客行为,例如处理

RequestBringIntoView
事件,但这并不可取,因为它会破坏程序化滚动。

该怎么办?

wpf treeview horizontal-scrolling
1个回答
0
投票

要控制水平滚动,请执行以下操作(我的树视图的名称是“内部”)

public ScrollViewer ScrollViewer { get; set; } = null;
public double ScrollViewerVerticalOffSet { get; set; }
private double HorizontalScrollPosition { get; set; } = 0.0;
private bool ResetHorizontalScrollPosition { get; set; } = false;

private void Internal_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
    if (ScrollViewer == null) SetScrollViewer();
    ResetHorizontalScrollPosition = true;
    HorizontalScrollPosition = ScrollViewer.HorizontalOffset;
}
private void SetScrollViewer()
{
    ScrollViewer = (ScrollViewer)Internal.Template.FindName("_tv_scrollviewer_", Internal);
    if (ScrollViewer != null) ScrollViewer.ScrollChanged += new ScrollChangedEventHandler(ScrollChanged);
}
private void ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (ResetHorizontalScrollPosition) ScrollViewer.ScrollToHorizontalOffset(HorizontalScrollPosition);
    ResetHorizontalScrollPosition = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.