none
TreeView + VirtualizingStackPanel + ScrollViewer Problem

    General discussion

  • When using the VirtualizedStackPanel as the ItemPanel for a TreeView and a vertical scroll bar is displayed, selecting an item in the TreeView causes the scrollviewer to jump significantly down the list. This is a big issue for me right now as my TreeView is unusable without virtualizing the ItemPanel and it's unusable after virtualizing because of this side effect. Any ideas? Any help is appreciated. Thanks.

    Wednesday, March 14, 2012 10:52 AM

All replies

  • Hi Doneholmes,

    To troubleshoot the issue, can you provide a simplified reproduce project here?

    Best Regards,

    Friday, March 16, 2012 2:22 AM
    Moderator
  • Hey, Silverlight (until Silverlight 4, not sure about 5) does NOT support UI virtualization for hierarchical data. 

    http://bea.stollnitz.com/blog/?p=338

    Friday, March 16, 2012 2:25 AM
  • Sorry it took so long for me to post this. Thanks for the interest. You'll find a reproducible code sample below...

    User Control XAML:

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplication12.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Height="400">
            <Grid.Resources>
                
                <sdk:HierarchicalDataTemplate x:Key="animalTemplate" ItemsSource="{Binding Path=Children}">
                    <TextBlock Text="{Binding Path=Name}" />
                </sdk:HierarchicalDataTemplate>
    
                <ItemsPanelTemplate x:Key="ItemPanelTemplate">
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
    
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
    <sdk:TreeView x:Name="treeView" ItemTemplate="{StaticResource animalTemplate}" ItemsPanel="{StaticResource ItemPanelTemplate}" />
        </Grid>
    </UserControl>


    User Control CodeBehind:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows.Controls;
    using System.Windows.Data;

    namespace SilverlightApplication12
    {
    public partial class MainPage : UserControl
    {
    private readonly CollectionViewSource _viewSource;
    public MainPage()
    {
    InitializeComponent();

    var animals = new Animals();
    treeView.ItemsSource = animals.AnimalList;
    }
    }
    public class Animal
    {
    private string name;

    public string Name
    {
    get { return name; }
    set { name = value; }
    }

    private Category category;

    public Category Category
    {
    get { return category; }
    set { category = value; }
    }

    public ObservableCollection<Animal> Children { get; set; }

    public Animal(string name, Category category)
    {
    Children = new ObservableCollection<Animal>();
    this.name = name;
    this.category = category;
    }
    }

    public enum Category
    {
    Amphibians,
    Bears,
    BigCats,
    Canines,
    Primates,
    Spiders,
    }

    public class Animals
    {
    private List<Animal> animalList;

    public IEnumerable<Animal> AnimalList
    {
    get { return animalList; }
    }

    public Animals()
    {
    animalList = new List<Animal>();
    animalList.Add(new Animal("California Newt", Category.Amphibians));
    animalList.Add(new Animal("Giant Panda", Category.Bears));
    var coyote = new Animal("Coyote", Category.Canines);
    for (var i = 0; i < 5; i++)
    {
    coyote.Children.Add(new Animal("Baby Coyote", Category.Canines));
    }
    animalList.Add(coyote);
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
    animalList.Add(new Animal("Mandrill", Category.Primates));
    animalList.Add(new Animal("Black Bear", Category.Bears));
    animalList.Add(new Animal("Jaguar", Category.BigCats));
    animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
    animalList.Add(new Animal("African Wildcat", Category.BigCats));
    animalList.Add(new Animal("Arctic Fox", Category.Canines));
    animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
    animalList.Add(new Animal("Grizzly Bear", Category.Bears));
    animalList.Add(new Animal("Dingo", Category.Canines));
    animalList.Add(new Animal("Gorilla", Category.Primates));
    animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
    animalList.Add(new Animal("Bald Uakari", Category.Primates));
    animalList.Add(new Animal("Polar Bear", Category.Bears));
    animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
    animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
    animalList.Add(new Animal("Cheetah", Category.BigCats));
    }
    }
    }
    Tuesday, March 20, 2012 1:58 PM
  • Hi Doneholmes,

    What is the GroupingTreeView class looks like inside the code you provided above?

    Best Regards,

    Wednesday, March 21, 2012 5:47 AM
    Moderator
  • Sorry, that was an artifact of refactoring. I consolodated those classes from another namespace for the sample and forgot to fix it in my pasted code. You should just remove any reference to that namespace.

    Thursday, March 22, 2012 2:32 PM