none
Custom Routed Events RRS feed

  • Question

  • Hi,

    I'm trying to pass some values in a Routed Event using a Custom RoutedEventArgs Class. In C# 2.0 I would create a Delegate to create a new event for my class with the custom EventArgs in the arguments section. How can I do this with RoutedEvents? I created a class that derives from RoutedEventArgs and I added the extra functionality I needed, now what do I have to do to create a RoutedEvent that takes those args?

    Thanks,
    Bruno Silva
    Multivector

    Monday, March 5, 2007 4:32 PM

Answers

  • 1. Create a delegate as follows

    public delegate void MyRoutedEventHandler(object sender, MyRoutedEventArgs e);

    2. Create custom event args

    public class MyRoutedEventArgs : RoutedEventArgs { ... }

    3. Define the event on your class which can fire the event

    class MyClass
    {
            public static readonly RoutedEvent MyRoutedEvent =
                EventManager.RegisterRoutedEvent("MyRouted", RoutingStrategy.Bubble, typeof(MyRoutedEventHandler), typeof(MyClass));

            public event MyRoutedEventHandler MyRouted
            {
                add { this.AddHandler(MyRoutedEvent, value); }
                remove { this.RemoveHandler(MyRoutedEvent, value); }
            }
    }

    4. Raise the event in your class as follows

    this.RaiseEvent(new MyRoutedEventArgs(...))

    Hope that helps!

    Regards
    Neil
    Monday, March 5, 2007 5:23 PM

All replies

  • 1. Create a delegate as follows

    public delegate void MyRoutedEventHandler(object sender, MyRoutedEventArgs e);

    2. Create custom event args

    public class MyRoutedEventArgs : RoutedEventArgs { ... }

    3. Define the event on your class which can fire the event

    class MyClass
    {
            public static readonly RoutedEvent MyRoutedEvent =
                EventManager.RegisterRoutedEvent("MyRouted", RoutingStrategy.Bubble, typeof(MyRoutedEventHandler), typeof(MyClass));

            public event MyRoutedEventHandler MyRouted
            {
                add { this.AddHandler(MyRoutedEvent, value); }
                remove { this.RemoveHandler(MyRoutedEvent, value); }
            }
    }

    4. Raise the event in your class as follows

    this.RaiseEvent(new MyRoutedEventArgs(...))

    Hope that helps!

    Regards
    Neil
    Monday, March 5, 2007 5:23 PM
  • Oops, I didn't thought the usual Delegate would work :)

    Thanks Neil

    Monday, March 5, 2007 5:31 PM