WPF TreeView with IsVirtualizing=“true” jumps arou

2019-02-21 09:36发布

问题:

I've made a simple example to reproduce a problem I'm having with the TreeView. If I select an item in the TreeView, scroll down, change the Focus, and then select an item in the TreeView again, the list starts jumping around.

This only happens if I have VirtualizingStackPanel.IsVirtualizing="True". Is this a known problem? Is there a work around?

So to reproduce:

  1. Select Item 1 in the list.
  2. Scroll down to the bottom of the list
  3. Click the TextBox up top.
  4. Select Item 30 in the list

I'm using .Net 3.5 SP1 and I've tried it for 64bit XP and Vista. I haven't tried it on other environments.

Update: I've tested in .Net 4.0 Beta and the issue doesn't occur. Is there a hotfix somewhere?


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0"></TextBox>

        <TreeView Grid.Row="1"
                  VirtualizingStackPanel.IsVirtualizing="True"
                  VirtualizingStackPanel.VirtualizationMode="Standard"
                  >          
            <TreeView.Items>
                <TreeViewItem Header="Header 1" IsExpanded="True">
                    <TreeViewItem.Items>
                        <TreeViewItem Header="Item 1" />
                        <TreeViewItem Header="Item 2" />
                        <TreeViewItem Header="Item 3" />
                        <TreeViewItem Header="Item 4" />
                        <TreeViewItem Header="Item 5" />
                        <TreeViewItem Header="Item 6" />
                        <TreeViewItem Header="Item 7" />
                        <TreeViewItem Header="Item 8" />
                        <TreeViewItem Header="Item 9" />
                        <TreeViewItem Header="Item 10" />
                        <TreeViewItem Header="Item 11" />
                        <TreeViewItem Header="Item 12" />
                        <TreeViewItem Header="Item 13" />
                        <TreeViewItem Header="Item 14" />
                        <TreeViewItem Header="Item 15" />
                        <TreeViewItem Header="Item 16" />
                        <TreeViewItem Header="Item 17" />
                        <TreeViewItem Header="Item 18" />
                        <TreeViewItem Header="Item 19" />
                        <TreeViewItem Header="Item 20" />
                        <TreeViewItem Header="Item 21" />
                        <TreeViewItem Header="Item 22" />
                        <TreeViewItem Header="Item 23" />
                        <TreeViewItem Header="Item 24" />
                        <TreeViewItem Header="Item 25" />
                        <TreeViewItem Header="Item 26" />
                        <TreeViewItem Header="Item 27" />
                        <TreeViewItem Header="Item 28" />
                        <TreeViewItem Header="Item 29" />
                        <TreeViewItem Header="Item 30" />
                    </TreeViewItem.Items>                    
                </TreeViewItem>
            </TreeView.Items>
        </TreeView>
    </Grid>
</Window>

Any help would be much appreciated!!!

回答1:

Well, it looks like a bug and there is a plenty of hotfixes. Do you have Windows Update enabled?