Hello, I'm working with a treeview and I want to remove al items and load another ones. To remove, i use in code behind treeview = null

Definition of Treeview in XAML:

<TreeView Name="tv" AllowDrop="False" DataContext="{x:Null}" Background="AliceBlue"
ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Source={StaticResource MyModel}}">

Where MyModel is a list of data created with HierarchicalDataTemplate.

Before tv = null, treeview still displaying data although tv has no data.

Can anybody halp me


Re: Windows Presentation Foundation (WPF) refresh treeview

Yi-Lun Luo - MSFT

Try tv.ItemsSource=null;

Also, TreeView.Items, which is of type ItemCollection, has a Refresh() method, which will recreate the CollectionView.

Hope this can solve your 2 problems.

Re: Windows Presentation Foundation (WPF) refresh treeview


Yeah!! it works!! thanks Yi, but... uff, tree data is deleted, but now, how can i display data again

Look at this, please. PostID=1401389&SiteID=1

Re: Windows Presentation Foundation (WPF) refresh treeview


Ok, enerything's ok. Thanks Yi-Lun Luo.

The solution is in ItemsSource. In XAML i don't define ItemsSource in the definition of the treeview. But in code behind, when I call to the constructor of my class, whinch inherit from ObservablsCollection<>, after I assignes it call to itemssource of the treeview, an when I want to remove the treeview, asiigned null to the treeview.