1

If i open a TreeView Item which Header is longer than the control, the scrollbar automatically jumps to the end of this item, which can be very disturbing... enter image description here

Any workaround so that the scrollbar dont jump "automatically"?

Jul Pod
  • 370
  • 3
  • 16

2 Answers2

3

You could try to handle the RequestBringIntoView of the TreeViewItem containers:

<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;

But please remember to always provide a Minimal, Complete, and Verifiable example of your issue when asking a question.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Well, you can handling the ScrollChanged event from ScrollViewer inside your TreeView.

So, you can write these lines of code here:

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);
Christian Amado
  • 946
  • 1
  • 7
  • 31