RadTreeViewItem Unloaded

5 posts, 0 answers
  1. James
    James avatar
    136 posts
    Member since:
    Nov 2008

    Posted 27 May 2010 Link to this post

    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.
  2. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 01 Jun 2010 Link to this post

    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.
  3. James
    James avatar
    136 posts
    Member since:
    Nov 2008

    Posted 02 Jun 2010 Link to this post

    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.
  4. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 04 Jun 2010 Link to this post

    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.
  5. James
    James avatar
    136 posts
    Member since:
    Nov 2008

    Posted 07 Jun 2010 Link to this post

    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.
Back to Top