这是正在发生的事情:
期望的结果:
有一些残酷的黑客行为,例如处理
RequestBringIntoView
事件,但这并不可取,因为它会破坏程序化滚动。
该怎么办?
要控制水平滚动,请执行以下操作(我的树视图的名称是“内部”)
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;
}