Skip to content

Commit e2b4caa

Browse files
authored
Adding sample of data virtualization (#3645)
1 parent 74b3f37 commit e2b4caa

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/MainDemo.Wpf/Domain/TreesViewModel.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.ObjectModel;
33
using System.Windows.Documents;
4+
using CommunityToolkit.Mvvm.ComponentModel;
45
using MaterialDesignDemo.Shared.Domain;
56
using MaterialDesignThemes.Wpf;
67

@@ -48,12 +49,24 @@ public class Planet
4849
public double Velocity { get; set; }
4950
}
5051

51-
public class TestItem
52+
public partial class TestItem : ObservableObject
5253
{
5354
public TestItem? Parent { get; set; }
5455
public string Name { get; }
5556
public ObservableCollection<TestItem> Items { get; }
5657

58+
// This property is used to determine if the item is expanded or not.
59+
// With the TreeListView control, the UI items are virtualized. Without
60+
// this property, the IsExpanded state of the TreeListViewItem would be lost
61+
// when it is recycled.
62+
//
63+
// For more information see:
64+
// https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
65+
//
66+
// https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
67+
[ObservableProperty]
68+
private bool _isExpanded;
69+
5770
public TestItem(string name, IEnumerable<TestItem> items)
5871
{
5972
Name = name;

src/MainDemo.Wpf/Trees.xaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@
231231
</DataTemplate>
232232
</materialDesign:TreeListView.Resources>
233233

234+
<!--
235+
Because Data Virtualization is enabled on this tree view by default, if you don't bind the IsExpanded property to something in the bound view model,
236+
you can loose the expanded state of items when the TreeListViewItem is recycled.
237+
238+
For more information:
239+
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
240+
https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
241+
-->
242+
<materialDesign:TreeListView.ItemContainerStyle>
243+
<Style TargetType="materialDesign:TreeListViewItem" BasedOn="{StaticResource {x:Type materialDesign:TreeListViewItem}}">
244+
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
245+
</Style>
246+
</materialDesign:TreeListView.ItemContainerStyle>
234247
</materialDesign:TreeListView>
235248
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
236249
<Button Command="{Binding AddListTreeItemCommand}"

0 commit comments

Comments
 (0)