none
TreeView Expanded

    Question

  • Hi!

    I am developing a custom WPF-Tree because of the lack of an expanded-event on the hole treeView. As I have seen this expanded-event only exists on TreeViewItems. Problem registering on all this TreeViewItems is, that these are binded via ItemsSource (ObservableCollection). So, how can I register events on TreeViewItem, if I don't have this items directly??

    Here a short example of my tree:

          public class MyTree : TreeView 
        { 
            private delegate void ExpandedHandler(object sender, RoutedEventArgs e); 
            public event ExpandedHandler Expanded; 
     
            public MyTree(ObservableCollection<TreeViewContent> content) 
            { 
                this.ItemsSource = content;         // data 
                this.ItemTemplate = itemTemplate;   // presentation 
     
                // here register expanded-event on every TreeViewItem?! 
            } 
     
            void treeViewItem_Expanded(object sender, RoutedEventArgs e) 
            { 
                Expanded.Invoke(sender, e); 
            } 
        } 

    or is there any other posibility for this?!?

    thx
    Dominik
    • Edited by DSchiefer Wednesday, November 12, 2008 5:47 PM
    Wednesday, November 12, 2008 5:37 PM

Answers

  • Hi,
    You do in XAML as:
    <TreeView Name="treeView1" TreeViewItem.Expanded="treeView1_Expanded"

    In code I believe you have to use this:

    treeView1.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(treeView1_Expanded)); 

    If you are doing this inside your class, just use the this.AddHandler();

    I actually have not done this in code, but that is how I would approach it.


    Hope this helps!

    -noorbakhsh





    • Edited by noorbakhsh Wednesday, November 12, 2008 6:38 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    Wednesday, November 12, 2008 6:37 PM

All replies

  • It depends on why you need this event.
    If you really need it, register to all Expanded events on all treeviewitems at once by using:
    EventManager.RegisterClassHandler(typeof(TreeViewItem),TreeViewItem.ExpandedEvent, your_delegate); 
    However, it seemed, that it can make sense to read about triggers and states in WPF. This may bring you new ideas

    Tamir http://blogs.microsoft.co.il/blogs/tamir
    If your question was answered, please mark it.
    Wednesday, November 12, 2008 6:32 PM
  • Hi,
    You do in XAML as:
    <TreeView Name="treeView1" TreeViewItem.Expanded="treeView1_Expanded"

    In code I believe you have to use this:

    treeView1.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(treeView1_Expanded)); 

    If you are doing this inside your class, just use the this.AddHandler();

    I actually have not done this in code, but that is how I would approach it.


    Hope this helps!

    -noorbakhsh





    • Edited by noorbakhsh Wednesday, November 12, 2008 6:38 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    • Marked as answer by DSchiefer Wednesday, November 12, 2008 9:54 PM
    Wednesday, November 12, 2008 6:37 PM
  • Sorry Tamir I had the window open and did not see you answered it already!

    Just as a helper for Dominik:

    In the TreeViewItem event you can always get to the TreeView by going up the visual tree. Also this way any item will trigger the same event, so in fact this will be sort of an event for the 'TreeView' if you want to think as it that way.

    -noorbakhsh
    Wednesday, November 12, 2008 6:44 PM