0

I want the default value of VirtualizingPanel.VirtualizationMode to Recycling, mainly on TreeViewItems. It may be something simple I'm missing but I've tried all these:

<TreeView VirtualizingPanel.IsVirtualizing="True"
          VirtualizingPanel.CacheLengthUnit="Pixel"
          VirtualizingPanel.VirtualizationMode="Recycling"/>

<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
    <Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling" />
</Style>
static MainWindow()
{
    VirtualizingPanel.VirtualizationModeProperty.OverrideMetadata(
        typeof(VirtualizingPanel), new FrameworkPropertyMetadata(VirtualizationMode.Recycling));
    VirtualizingPanel.VirtualizationModeProperty.OverrideMetadata(
        typeof(MainWindow), new FrameworkPropertyMetadata(VirtualizationMode.Recycling));
    VirtualizingPanel.VirtualizationModeProperty.OverrideMetadata(
        typeof(TreeViewItem), new FrameworkPropertyMetadata(VirtualizationMode.Recycling));
    VirtualizingPanel.VirtualizationModeProperty.OverrideMetadata(
        typeof(VirtualizingStackPanel), new FrameworkPropertyMetadata(VirtualizationMode.Recycling));
}

But it all seems to do nothing. According to the Xaml live property explorer. enter image description here

How to reproduce:

  • Download and run the source code from github in Visual Studio in debug mode.
  • Open the "Live Visual Tree" window in Visual Studio and find a VirtualizingStackPanel under some TreeViewItem.
  • Right-click, select properties, and find the VirualizationMode property.
Hossein Ebrahimi
  • 632
  • 10
  • 20
  • Are you not binding itemssource on the treeview? If you set it directly or add items in xaml/code then I don't think it can virtualize. – Andy Jan 08 '23 at 16:40
  • ItemsSource is bound, I just removed that from the code snippet. – Hossein Ebrahimi Jan 08 '23 at 16:42
  • Please update your question to show how someone would reproduce your issue. – Andy Jan 08 '23 at 17:00
  • I did. You can get my code from github, it's a fairly simple app just to reproduce this issue. Please ask for more specific info if missing. – Hossein Ebrahimi Jan 08 '23 at 17:02
  • @HosseinEbrahimi: Setting the `VirtualizingPanel.VirtualizationMode` attached property of the `TreeView` is good enough. If you look at a `TreeViewItem`, you should note that it has a `VirtualizationMode` of `Recycling` at runtime. What is your issue? – mm8 Jan 09 '23 at 21:47
  • @mm8 The snapshot in my question is taken from the `VirtualizingStackPanel` under a `TreeViewItem`. It shows that `VirtualizationMode` is still `Standard`. – Hossein Ebrahimi Jan 09 '23 at 21:55
  • It's unclear from where and how the snapshot in your question is taken. How to reproduce? – mm8 Jan 09 '23 at 21:57

0 Answers0