垂直滚动条自动“跳跃”#34;向前

时间:2017-06-13 14:41:07

标签: c# wpf

如果我打开TreeView Item Header比控件长,滚动条会自动跳转到此项目的末尾,这可能非常令人不安...... enter image description here

任何解决方法,以便滚动条不会自动跳转"?

2 个答案:

答案 0 :(得分:2)

您可以尝试处理RequestBringIntoView容器的TreeViewItem

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <EventSetter Event="RequestBringIntoView" Handler="OnRequestBringIntoView"/>
        </Style>
    </TreeView.ItemContainerStyle>
    ...
</TreeView>
private void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) => e.Handled = true;

但请记住在提问时始终提供a Minimal, Complete, and Verifiable example您的问题。

答案 1 :(得分:0)

好吧,您可以从 TreeView 中的 ScrollViewer 处理 ScrollChanged 事件。

所以,你可以在这里写下这些代码:

var element1 = VisualTreeHelper.GetChild(sender as TreeView, 0) as X; 
// Where X is the object inside your TreeView. Eg. StackPanel, Border...

var scrollviewer = VisualTreeHelper.GetChild(element1, 0) as ScrollViewer;
//Goes to the beggining of your content
scrollviewer.ScrollToLeftEnd();

// Another way is...
scrollViewer.ScrollToHorizontalOffset(e.HorizontalOffset + e.ExtentHeightChange);