This is a migrated thread and some comments may be shown as answers.

RadTreeViewItem Unloaded

4 Answers 28 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 27 May 2010, 10:13 AM
Hello,

What is the best way to detect when a TreeViewItem is removed from the TreeView?

I'm looking for the opposite of ItemPrepared e.g. ItemRemoved

I've tried subscribing to the Unloaded event but this does not fire when the item is removed from the TreeView i.e.

    public partial class MainPage : UserControl {  
        ObservableCollection<Person> people = new ObservableCollection<Person>();  
        public MainPage() {  
            InitializeComponent();  
 
            people.Add(new Person() {FirstName = "Adam"});  
            people.Add(new Person() { FirstName = "Eve" });  
 
            this.mainRadTreeView.ItemPrepared += new EventHandler<Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs>(mainRadTreeView_ItemPrepared);  
            this.mainRadTreeView.ItemsSource = this.people;  
        }  
 
        void mainRadTreeView_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e) {  
            e.PreparedItem.Unloaded += new RoutedEventHandler(PreparedItem_Unloaded);  
        }  
 
        void PreparedItem_Unloaded(object sender, RoutedEventArgs e) {  
            // This is never handled.  
        }  
 
        private void Button_Click(object sender, RoutedEventArgs e) {  
            this.people.RemoveAt(this.people.Count-1);  
        }  
    }  
 
    public class Person {  
        public string FirstName { getset; }  
    } 

Help appreciated,

James.

4 Answers, 1 is accepted

Sort by
0
Bobi
Telerik team
answered on 01 Jun 2010, 02:25 PM
Hi James,

Please find attached a sample project that demonstrates the usage of data bound treeview with Unloaded event.
  Here you can find the full list of supported events:
http://www.telerik.com/help/wpf/radtreeview-events-overview.html

If you have data bound treeview and want to remove some item from the collection you can also try something like:
 collection.CollectionChanged+=new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);

I hope that this will help you. Please let us know if you have any other questions or need some more help.

Greetings,
Bobi
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
James
Top achievements
Rank 1
answered on 02 Jun 2010, 08:34 AM
Thanks Bobi,

If I set IsVirtualizing to True then the Unloaded event in your example does not fire (I overlooked this in my original post).

<treeview:RadTreeView x:Name="treeView" IsVirtualizing="True" /> 


Is this expected behavior?

I can work around this in my application, but I really need the unloaded event to fire for each RadTreeViewItem so if turning off virutalization is the only option then fine.

Regards,

James.
0
Miroslav
Telerik team
answered on 04 Jun 2010, 03:12 PM
Hi James,

The Unloaded event fires when a visual object is removed from the VisualTree. This does not always happen with virtualization enabled since containers are reused and may not be removed from the VisualTree.

There is a 'cleanup' event that you can handle.

treeView.AddHandler(TreeViewPanel.CleanUpVirtualizedItemEvent, 
    new Telerik.Windows.Controls.TreeView.CleanUpVirtualizedItemEventHandler(OnCleanup), true);

and the handler:

public void OnCleanup(object sender, Telerik.Windows.Controls.TreeView.CleanUpVirtualizedItemEventArgs e)
{
}

Do you mind telling us why you need the unloaded event?

All the best,
Miroslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
James
Top achievements
Rank 1
answered on 07 Jun 2010, 07:05 AM
Thanks for the tip.

I need to remove a DispatcherTimer that is running within a custom control that is displayed as the Content for each RadTreeViewItem.

If this does not occur then the custom control remains in memory after the RadTreeViewItem is removed from the tree.

James.
Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Bobi
Telerik team
James
Top achievements
Rank 1
Miroslav
Telerik team
Share this question
or