Does Binding work ONLY with DependencyProperty?

放肆的年华 提交于 2021-02-06 10:39:07

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4316174/does-binding-work-only-with-dependencyproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!