File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System . Collections ;
2
2
using System . Collections . ObjectModel ;
3
3
using System . Windows . Documents ;
4
+ using CommunityToolkit . Mvvm . ComponentModel ;
4
5
using MaterialDesignDemo . Shared . Domain ;
5
6
using MaterialDesignThemes . Wpf ;
6
7
@@ -48,12 +49,24 @@ public class Planet
48
49
public double Velocity { get ; set ; }
49
50
}
50
51
51
- public class TestItem
52
+ public partial class TestItem : ObservableObject
52
53
{
53
54
public TestItem ? Parent { get ; set ; }
54
55
public string Name { get ; }
55
56
public ObservableCollection < TestItem > Items { get ; }
56
57
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
+
57
70
public TestItem ( string name , IEnumerable < TestItem > items )
58
71
{
59
72
Name = name ;
Original file line number Diff line number Diff line change 231
231
</DataTemplate >
232
232
</materialDesign : TreeListView .Resources>
233
233
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>
234
247
</materialDesign : TreeListView >
235
248
<StackPanel Orientation =" Horizontal" VerticalAlignment =" Bottom" HorizontalAlignment =" Right" >
236
249
<Button Command =" {Binding AddListTreeItemCommand}"
You can’t perform that action at this time.
0 commit comments