Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sample of data virtualization with TreeListView #3645

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading