Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Binding work ONLY with DependencyProperty?

MSDN says,

  • Each binding typically has these four components: a binding target object, a target property, a binding source, and a Path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

  • The target property must be a dependency property.

The above excerpt explains why the following code works,

<TextBox Text="{Binding EmployeeName}">

It works because Text is a dependency property. Upto this point, everything is fine!


My question is,

IF target property must be a dependency property for Binding to work, then how does the folllowing Setter work? Please note that Value in Setter is NOT a dependency property!

<Style TargetType="{x:Type TextBox}">
      <Setter Property="Text" Value="{Binding EmployeeName}"/>
</Style>

I've an explanation for it. But I'm not sure if that is correct. Let me first explain it, and then, you guys correct me if I'm wrong. :-)

I think, since the type of Value is Object, that means, it can hold instances of ANY type. It can hold an instance of even Binding type. It, however, cannot take part in the process of evaluating (or resolving) the Binding expression, since it's not a dependency property. So Style object simply transfers this Binding object (which Value holds) from Setter to the TextBox as such, without evaluating/resolving the Binding value. As such the above Setter becomes equivalent to this:

 Text="{Binding EmployeeName}"

And since now, Text is a dependency property, it can resolve the Binding value. So it first appears that target of binding is Value, but in reality, Text is the target of the binding.

It's like Setter is a postman, Value itself is postman's Bag, Binding instance (i.e what Value holds) is a Letter. Postman (i.e Setter) delivers the Letter(i.e Binding instance) to the Target (i.e Text property), without opening it, i.e without knowing what Message (i.e EmployeeName) the Letter carries.

Please correct me if I'm wrong.

like image 713
Nawaz Avatar asked Nov 30 '10 16:11

Nawaz


2 Answers

The XAML is setting Setter.Value to an object of type Binding. The Style thus created then sets the Text dependency property on the target object to that Binding, so that the binding will update Text whenever the source property changes.

If Setter.Value were a dependency property, then the setter would end up being an intermediary in property-change notification: the source property changes, the binding notifies the setter, the setter notifies the target. Since it's just a CLR property, the setter's not actually involved in change notification: the source property changes and the binding notifies the target.

like image 79
Robert Rossney Avatar answered Oct 12 '22 23:10

Robert Rossney


That's correct. If you look at the source code of WPF toolkit controls you can see how it's done. The DataGrid does it in quite a few places (e.g. specific columns passing the Content binding to the cell). I'll try to remember to add a link to a specific file and line where it's done after work.

Edit A good example is the Binding property in the DataGridBoundColumn class. You can see it used in

internal void ApplyBinding(DependencyObject target, DependencyProperty property)

If you're interested in advanced patterns to make WPF and Silverlight controls I highly recommend looking through their respective toolkits. They're well-commented in general and some of the controls are using some cool code.

like image 27
Bryan Anderson Avatar answered Oct 13 '22 00:10

Bryan Anderson