locked
Routed events in c# WPF RRS feed

  • Question

  •  I have searched for the concept of routed events but ..i still have confusion ..most of the time i create my own custome events using delegates ..can enyone plz elaborate the concept of routed events in wpf ,Why do we need it ? when do we use then ? how does it work?  Plz let me know...


    Friday, June 22, 2012 5:43 AM

Answers

  • Hi Manish Kohale,

    Let me try to explain to you about the Routed Event.

    For what is it, and when we use it, Routed events are events that are designed to work well with a tree of elements. When a routed event is raised, it can travel up or down the visual and logical tree, getting raised on each element in a simple and consistent fashion, without the need for any custom code. So simplify speaking, routed event can help us to remain oblivious to details of the Visual Tree of WPF control. Such as you can create a very control template for Button, but in the end, you still can click it to fire the Click event of button.

    Hope it helps.

    Best regards. 


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us


    Friday, June 29, 2012 5:25 AM

All replies

  • Hi Manish Kohale

    To be short and precise.

    Let me take an example of a button firing an event.

    1. As we know with ControlTemplate the Button is made up of

    some other different controls(Say 2 ellipse and an image in our case). 

    2. when you click on a button it might be any one of the above in controlTemplate.

        The event keep bubbling as long as you are not handle it.

         a. say you clicked in image(at depth 3) now,

              1. corresponding event on image not handled

              2. Go For ellipse 1 which embeds image  again say not handled

              3. Go For ellipse 2 which embeds ellipse-2 . again say not handled

              now event will be fired on your button. which you might handle

    I have tried in my way. If you find the other please let it be posted.

    Regards

    Agrawal

     

    Friday, June 22, 2012 7:29 AM
  • Hi Manish Kohale,

    Let me try to explain to you about the Routed Event.

    For what is it, and when we use it, Routed events are events that are designed to work well with a tree of elements. When a routed event is raised, it can travel up or down the visual and logical tree, getting raised on each element in a simple and consistent fashion, without the need for any custom code. So simplify speaking, routed event can help us to remain oblivious to details of the Visual Tree of WPF control. Such as you can create a very control template for Button, but in the end, you still can click it to fire the Click event of button.

    Hope it helps.

    Best regards. 


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us


    Friday, June 29, 2012 5:25 AM
  •  Does that mean If i have created a button with Rectangle and a image,and i want to fire event on click of this button. it would be fired by every elemnt (rectangle,Image and button), but as a whole these elemnts ( rectangle,image) makes the button it will fire combinely one evnt. ..?


    is actually 

     

     Rply..
    Wednesday, July 4, 2012 5:21 AM
  • Yes, Manish, things almost like that.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 5, 2012 9:48 AM
  •  k ...gotcha..

     Thank you Annabella..

     

    Thursday, July 5, 2012 1:08 PM