3

Possible Duplicate:
Has an event handler already been added?

Firstly I know there are a number of simmilar questions but I am having trouble adapting them to my situation.

I would like to be able to find out which event handler is assigned for a paticular event on an object. So on a treeviewitem, i will have assigned a handler for the expanded event. I would then at a later date like to be able to pull the assigned event handler out and assign that to another treeviewitems expanded event so they both call the same handler on the expanded event.

Is this possible?

Heres what I want to be able to do...

private void duplicateHandler( TreeViewItem existingItem )
{
    TreeViewItem tvi = new GX3TreeViewItem();

    tvi.AddHandler(TreeViewItem.ExpandedEvent, existingItem.ExpandedEventHandler);
}
Community
  • 1
  • 1
user589195
  • 4,180
  • 13
  • 53
  • 81
  • I have looked at that but have a # of questions about it. Which class would that method go in? An extended treeviewitem class? If so how so I get the assigned Expanded Handler? – user589195 Dec 11 '12 at 09:58

2 Answers2

3
  1. create a new WPF app
  2. put a treeview named "tree1" in your mainwindow
  3. copy-paste the code which follows to see it in action

Basically, you need to inherit TreeViewItem and build a mechanism to keep track of which handlers is being added to which node: event has no information about registered handlers for you to retrieve, unfortunately.

You're also going to need to rely exclusively on your new inheritance to build nodes, for things to work properly. That means, you must no longer refer to TreeViewItem, but have to refer to TreeNode (this is the name I chose for the custom class, feel free to change it).

Promised code is below, what it does is adding a "rootNode" node to the tree, which upon expansion would create a sibling node, which would have the same handler brought over to itself (so it too would create a sibling upon expansion, and so on).

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TreeNode root = new TreeNode();
            root.Header = "root";

            tree1.Items.Clear();

            // add node to tree before adding handlers, or you'll get
            // a StackOverflowException
            tree1.Items.Add(root);

            root.AddHandler(TreeNode.ExpandedEvent, new RoutedEventHandler(expandedHandler));

        }

        private void expandedHandler(object sender, RoutedEventArgs e) { newNodeCopyExpandedHandlers(sender as TreeNode); }

        private void newNodeCopyExpandedHandlers(TreeNode node)
        {
            TreeNode newNode = new TreeNode();
            newNode.Header = "nuovo!";

            // add node to tree before adding handlers, or you'll get
            // a StackOverflowException
            tree1.Items.Add(newNode);

            foreach (Delegate d in newNode.GetHandlers(node, TreeNode.ExpandedEvent))
                newNode.AddHandler(TreeNode.ExpandedEvent, d);
        }
    }

    public class TreeNode : TreeViewItem
    {
        private Dictionary<RoutedEvent, List<Delegate>> handlersList = new Dictionary<RoutedEvent, List<Delegate>>();

        public new void AddHandler(RoutedEvent e, Delegate d)
        {
            if (!handlersList.ContainsKey(e)) handlersList.Add(e, new List<Delegate>());
            handlersList[e].Add(d);

            base.AddHandler(e, d);
        }
        public new void RemoveHandler(RoutedEvent e, Delegate d)
        {
            if (!handlersList.ContainsKey(e)) handlersList.Add(e, new List<Delegate>());
            if (handlersList[e].Contains(d)) handlersList[e].Remove(d);

            base.RemoveHandler(e, d);
        }

        public List<Delegate> GetHandlers(TreeNode n, RoutedEvent e)
        {
            if (n.handlersList.ContainsKey(e)) return n.handlersList[e];
            else return null;
        }
    }
}
Alex
  • 23,004
  • 4
  • 39
  • 73
-1

Yes sure you can, you can navigate through your code until you find the event assignment:

event +=  myEventHandler

You can then use the same method myEventHandler to subscribe events on other trees. Is this the problem or I'm misunderstanding the question?

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69