Skip to content

Commit

Permalink
Adding sample of data virtualization (#3645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo authored Aug 7, 2024
1 parent 74b3f37 commit e2b4caa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/MainDemo.Wpf/Domain/TreesViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Documents;
using CommunityToolkit.Mvvm.ComponentModel;
using MaterialDesignDemo.Shared.Domain;
using MaterialDesignThemes.Wpf;

Expand Down Expand Up @@ -48,12 +49,24 @@ public class Planet
public double Velocity { get; set; }
}

public class TestItem
public partial class TestItem : ObservableObject
{
public TestItem? Parent { get; set; }
public string Name { get; }
public ObservableCollection<TestItem> Items { get; }

// This property is used to determine if the item is expanded or not.
// With the TreeListView control, the UI items are virtualized. Without
// this property, the IsExpanded state of the TreeListViewItem would be lost
// when it is recycled.
//
// For more information see:
// https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
//
// https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
[ObservableProperty]
private bool _isExpanded;

public TestItem(string name, IEnumerable<TestItem> items)
{
Name = name;
Expand Down
13 changes: 13 additions & 0 deletions src/MainDemo.Wpf/Trees.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@
</DataTemplate>
</materialDesign:TreeListView.Resources>

<!--
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,
you can loose the expanded state of items when the TreeListViewItem is recycled.
For more information:
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
-->
<materialDesign:TreeListView.ItemContainerStyle>
<Style TargetType="materialDesign:TreeListViewItem" BasedOn="{StaticResource {x:Type materialDesign:TreeListViewItem}}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</Style>
</materialDesign:TreeListView.ItemContainerStyle>
</materialDesign:TreeListView>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Button Command="{Binding AddListTreeItemCommand}"
Expand Down

0 comments on commit e2b4caa

Please sign in to comment.