Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am just going through the WPF concepts I came across these routed events, dependency properties and attached events.

What are the concepts behind them and why are they called instead of .NET ordinary methods?

share|improve this question
up vote 15 down vote accepted

Routed events

According to MSDN,

A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

The definition is followed by an example of a WPF node with three buttons. The single routed event is listening for three buttons. If you try to write the same code using ordinary events, you'll have to use three separated events, one event by button.

This means that, instead of defining multiple events on every control in a same child, you have to define a routed event at the parent level, which will be raised by every concerned child.

Further, you may want to read Routed Events on WPFTutorial.net which uses a more precise terminology than my answer, and which also explains why routed events usually appear as pair.

Dependency properties

According to MSDN,

The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs.

Think it as a way to simplify your code (and especially the ability to use the code), since you can use a dependency property as an ordinary one.

Further, you may want to read a more complete article on Switch On The Code article, which also explains why it makes sense that a DependencyProperty is static, shows how to add change callbacks or use validation, etc.

Attached events

According to MSDN,

The concept of an attached event enables you to add a handler for a particular event to an arbitrary element rather than to an element that actually defines or inherits the event.

This is particularly useful when the object which raises the event is independent from the actual WPF tree. One of the examples given by the same documentation is the mouse events, when those events are not related to the WPF element which, like in case of MouseDown, both raises and listens to the event.

Further, you may want to read Attached Events By Example, which explains that "the class defining the event is often neither the user of the event (whoever consumes it by adding a handler on it) nor the source of the event (whatever code raises it).", and gives much more details about attached events and their difference with routed events.

share|improve this answer
1  
thank you very much help me a lot – judith Jan 18 '12 at 14:35
    
"A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event." isn't a listener and a handler the same thing? this MSDN answer while technically perfect is not helping with the grasping of this concept :'( any ideas? – BKSpurgeon Mar 1 at 1:10

You can already do this with regular eventargs by placing the same event handler name in several button click properties and then converting sender to button to access it's properties. You can also already call a button click event from some arbitrary element by way of using a delegate so there again, not sure how this is better, it's just different is all I can make of it.

share|improve this answer

protected by gnat Feb 4 at 16:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.