绑定仅与DependencyProperty一起使用吗? [英] Does Binding work ONLY with DependencyProperty?

查看:76
本文介绍了绑定仅与DependencyProperty一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN说,

  • 每个绑定通常具有以下四个组成部分:绑定目标对象,目标属性,绑定源,以及指向该值的路径要使用的绑定源.例如,如果您想绑定一个内容TextBox的名称属性员工对象,您的目标对象是文本框,目标属性是文字属性,则使用的值为名称,源对象是员工对象.

  • 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 above excerpt explains why the following code works,

<TextBox Text="{Binding EmployeeName}">

它起作用是因为 Text 是一个依赖项属性.到目前为止,一切都很好!

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

我的问题是

如果目标属性必须是绑定的依赖项属性,那么随后的 Setter 将如何工作?请注意,Setter中的不是依赖项属性!

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. :-)

我认为,由于 Value type Object ,这意味着它可以容纳ANY类型的实例.它可以包含甚至Binding类型的实例.但是,由于它不是依赖项属性,因此它不能参与评估(或解析)Binding表达式的过程.因此,Style对象只是将此Binding对象(Value保留)从Setter传递到这样的TextBox,而无需评估/解析Binding值.因此,以上的Setter等效于此:

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}"

从现在开始,Text是一个依赖项属性,它可以解析Binding值.因此,首先看起来绑定的目标是值,但实际上,文本是绑定的目标.

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.

这就像塞特是邮递员,价值本身就是邮递员的包,绑定实例(即价值持有的东西)是字母.邮递员(即Setter)将Letter(即Binding实例)传递给目标(即Text属性),而没有打开它,即不知道信件携带什么消息(即EmployeeName).

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.

推荐答案

XAML将 Setter.Value 设置为 Binding 类型的对象.这样创建的 Style 然后将目标对象上的 Text 依赖项属性设置为该 Binding ,以便绑定将更新 Text 只要源属性发生更改.

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.

如果 Setter.Value 是依赖项属性,则该setter最终将成为属性更改通知的中介:源属性发生更改,绑定通知setter,setter通知目标.由于它只是CLR属性,因此设置程序实际上并不参与更改通知:源属性会更改,而 binding 会通知目标.

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.

这篇关于绑定仅与DependencyProperty一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆