Martin2233

Hi,

For some reason I don't understand, when a horizontal scrollbar is displayed for a TreeView and user selects an item which is wider than available horizontal space, the TreeView automatically horizontally scrolls according to that item. I don't understand the reason or mechanics behind the scenes which are causing this behavior, but I'd like to turn it off. Please, help.

Thanks, Martin



Re: Windows Presentation Foundation (WPF) TreeView and undesired automatic horizontal scrolling

Matt Eland

Sounds like this is an interaction with the scrollviewer and the focus system.

When an element is focused within a ScrollViewer (which is part of the TreeView template), the ScrollViewer is instructed to make the element visible. It automatically responds by scrolling to the requested element.

The methods inside of ScrollViewer that handle these focus requests are all private and / or internal so you really can't get to them. I don't think there's too much you can do in this case; it's just how focus works.





Re: Windows Presentation Foundation (WPF) TreeView and undesired automatic horizontal scrolling

Martin Dvorak

Thanks for your answer, that makes sense. Unfortunately this behavior is very user-unfriendly, since the scrolling hides the expand/collapse handles of the tree items, which means that users must very often operate the horizontal scrollbar to make them appear.

Maybe a suggestion for next version of WPF - it would be great if this behavior could be turned off, so that the scrollbar always stays at last position manually set by the user.





Re: Windows Presentation Foundation (WPF) TreeView and undesired automatic horizontal scrolling

Martin Dvorak

For those who might encounter the same problem, here is a workaround which seems to work properly. Include some FrameworkElement derived element in your TreeViewItem template, for example Rectangle, give it name "PART_Header" and make sure it covers the whole line of the item it belongs to (including any space before the item using negative margin).

An incomplete example:

<!-- tree item template -->

<ControlTemplate TargetType="TreeViewItem">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<Rectangle Name="PART_Header" Grid.ColumnSpan="2" Margin="-100,1,0,1" Fill="Transparent" IsHitTestVisible="False"/>

<!-- header -->

<ContentPresenter Grid.Column="1" Margin="0,3,0,3" Height="15" IsHitTestVisible="False" ContentSource="Header"/>

<!-- toggle button -->

<ToggleButton Name="Expander" Grid.Column="0" />

<ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" />

</Grid>

</ControlTemplate>





Re: Windows Presentation Foundation (WPF) TreeView and undesired automatic horizontal scrolling

Martin Dvorak

For those who might encounter the same problem, here is a workaround which seems to work properly. Include some FrameworkElement derived element in your TreeViewItem template, for example Rectangle, give it name "PART_Header" and make sure it covers the whole line of the item it belongs to (including any space before the item using negative margin).

An incomplete example:

<!-- tree item template -->

<ControlTemplate TargetType="TreeViewItem">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<Rectangle Name="PART_Header" Grid.ColumnSpan="2" Margin="-100,1,0,1" Fill="Transparent" IsHitTestVisible="False"/>

<!-- header -->

<ContentPresenter Grid.Column="1" Margin="0,3,0,3" Height="15" IsHitTestVisible="False" ContentSource="Header"/>

<!-- toggle button -->

<ToggleButton Name="Expander" Grid.Column="0" />

<ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" />

</Grid>

</ControlTemplate>