I have an application with a tree and text field:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="550" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <TreeView Width="500" Height="500" Name="TestTreeView"> </TreeView> <TextBox Grid.Row="1"></TextBox> </Grid> </Window>
In the constructor for the application, I generate 1000 elements, each of which has 1 subposition and adds it to the TreeView:
public MainWindow() { InitializeComponent(); for (int i = 0; i < 1000; i++) { TreeViewItem item = new TreeViewItem(); item.Header = "Hey there " + i.ToString(); TreeViewItem subItem = new TreeViewItem(); subItem.Header = "Hi there"; item.Items.Add(subItem); TestTreeView.Items.Add(item); } }
In my script, I select the second TreeView element, and then click in the TextBox to divert attention from the TreeView. Then I look at the TreeView with the mouse, bringing the selected item out of the viewport. Then I expand another element without selecting it. My expected result is that my scroll remains in its current position, but instead, it takes the selected item back to view, causing me to lose the item that I was expanding.
This does not happen if TreeView already has focus. If I were to select an element, scroll down and expand another element, this behavior of the transition to the selected element will not happen.
Is this normal behavior? For example, if I perform the same selection steps in the Visual Studio Solution Explorer, I do not get this behavior. Is there a way to tell him not to scroll back to a selected item like this?
I understand that I can just set e.Handled from RequestBringIntoView to true, however the sample that I give just explains my problem. This is an example of a problem with TreeView that I am using in a much larger application where I want the element to appear under certain conditions.
source share