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 11using System . Collections ;
22using System . Collections . ObjectModel ;
33using System . Windows . Documents ;
4+ using CommunityToolkit . Mvvm . ComponentModel ;
45using MaterialDesignDemo . Shared . Domain ;
56using 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 ;
Original file line number Diff line number Diff line change 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}"
You can’t perform that action at this time.
0 commit comments