Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 9 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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