MSDN 说,
上面的摘录解释了以下代码的工作原理,
<TextBox Text="{Binding EmployeeName}">
它之所以有效,是因为 Text 是一个依赖属性。到目前为止,一切都很好!
我的问题是,
如果目标属性必须是 Binding 才能工作的依赖属性,那么以下 Setter 如何工作?请注意,Setter 中的 Value 不是依赖属性!
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding EmployeeName}"/>
</Style>
我对此有一个解释。但我不确定这是否正确。我先解释一下,如有错误,请大家指正。 :-)
我认为,由于 Value 的 type 是 Object,这意味着它可以保存任何类型的实例。它甚至可以保存 Binding 类型的实例。 但是,它不能参与计算(或解析)Binding 表达式的过程,因为它不是依赖属性。因此,Style 对象只是将此 Binding 对象(由 Value 保存)从 Setter 传输到TextBox 本身,而不评估/解析 Binding 值。因此,上面的 Setter 就相当于这样:
Text="{Binding EmployeeName}"
从现在起,Text 是一个依赖属性,它可以解析 Binding 值。 所以乍一看绑定的目标是Value,但实际上绑定的目标是Text。
这就好像Setter是邮递员,Value本身就是邮递员的Bag,Binding实例(即Value持有的东西) )是一封信。 Postman(即Setter)将Letter(即Binding实例)传递到Target(即Text属性),而无需打开它,即不知道Letter携带什么Message(即EmployeeName)。
如果我错了,请纠正我。
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.
发布评论
评论(2)
XAML 将
Setter.Value
设置为Binding
类型的对象。然后,创建的Style
将目标对象上的Text
依赖属性设置为该Binding
,以便绑定将更新Text< /code> 每当源属性更改时。
如果 Setter.Value 是依赖属性,那么 setter 最终将成为属性更改通知的中介:源属性更改,绑定通知 setter,setter 通知目标。由于它只是一个 CLR 属性,因此 setter 实际上并不参与更改通知:源属性更改,并且绑定会通知目标。
The XAML is setting
Setter.Value
to an object of typeBinding
. TheStyle
thus created then sets theText
dependency property on the target object to thatBinding
, so that the binding will updateText
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.这是正确的。如果您查看 WPF 工具包 控件的源代码,您可以看到它是如何完成的。 DataGrid 在很多地方都这样做(例如将内容绑定传递到单元格的特定列)。我会尽力记住在下班后添加指向特定文件和行的链接。
编辑
一个很好的例子是 DataGridBoundColumnBinding 属性a> 类。如果
您对制作 WPF 和 Silverlight 控件的高级模式感兴趣,我强烈建议您查看它们各自的工具包。它们总体上得到了很好的评论,并且一些控件使用了一些很酷的代码。
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 inIf 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.