Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Automatic Horizontal Scroll in TreeView

Tags:

wpf

Whenever a node is selected in my treeview, it automatically does a horizontal scroll to that item. Is there a way to disable this?

like image 371
Robert Avatar asked Jul 12 '10 04:07

Robert


2 Answers

Handle the RequestBringIntoView event and set Handled to true, and the framework won't try to bring the item into view. For example, do something like this in your XAML:

<TreeView>     <TreeView.ItemContainerStyle>         <Style TargetType="TreeViewItem">             <EventSetter Event="RequestBringIntoView" Handler="TreeViewItem_RequestBringIntoView"/>         </Style>     </TreeView.ItemContainerStyle> </TreeView> 

And then this in your code-behind:

private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) {     e.Handled = true; } 
like image 86
Quartermeister Avatar answered Sep 21 '22 07:09

Quartermeister


I managed to solve the problem using the following:

<TreeView ScrollViewer.HorizontalScrollBarVisibility="Hidden">    <TreeView.ItemsPanel>       <ItemsPanelTemplate>          <StackPanel MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=1}}" />       </ItemsPanelTemplate>    </TreeView.ItemsPanel> </TreeView> 

I bind the width of the StackPanel which renders the ItemsPanel here, to the ActualWidth of ContentPresenter in the TreeView.

It also works nice with the "hacked" Stretching TreeView by: http://blogs.msdn.com/b/jpricket/archive/2008/08/05/wpf-a-stretching-treeview.aspx (I modified that solution not to remove grid column, but to change Grid.Column property of the first Decorator element from 1 to 2).

like image 25
Dariusz Wasacz Avatar answered Sep 25 '22 07:09

Dariusz Wasacz