什么时候使用WPF绑定中的路径? [英] When to use Path in WPF Binding?

查看:268
本文介绍了什么时候使用WPF绑定中的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多WPF绑定示例,并且已经在许多不同的地方使用该功能来学习MVVM,但是与我似乎很不一致的是当你在绑定字符串中指定Path =时只需键入要绑定到的属性即可。例如,以下XAML属性之间的功能差异是什么?

  DataMemberBinding ={Binding SomeProperty}
DataMemberBinding ={Binding Path = SomeProperty}


解决方案

没有功能差异。 Binding对象的默认属性是Path,这意味着如果您不指定要设置的属性,那么您将设置Path。



这是因为Binding对象有两个构造函数,一个默认值,一个构造函数接受单个字符串参数。当您传递一个值而不标记它时,该属性将被转发到匹配的构造函数上,在Binding的情况下会设置路径。它在概念上与属性的工作方式非常相似,调用构造函数后跟可选参数,例如:

  AttributeUsage(AttributeTargets.Class),AllowMultiple = false,Inherited = false] 






可能方式超出了你实际提出的问题,但我注意到这两者之间存在轻微的(也许是无关紧要的)差异。由于我自己无法解释,我已经开始了一个新的问题。这里


I've seen a lot of WPF Binding examples and have used the feature in a lot of different places in learning MVVM, but something that has seemed quite inconsistent to me is when you specify "Path=" in the binding string as appose to simply typing in the property you want to bind to. For example, what's the functional difference between the following XAML attributes:

DataMemberBinding="{Binding SomeProperty}"
DataMemberBinding="{Binding Path=SomeProperty}"

解决方案

There is no functional difference. The default property of the Binding object is Path, this means if you don't specify which property you are setting then you will set Path.

This is because the Binding object has two constructors, one default and one that takes in a single string parameter. When you pass in a value without labeling it that property will be forwarded onto the matching constructor, in the case of Binding this sets the path. It is very similar in concept to the way attributes work, a call to the constructor followed by optional parameters, for example:

[AttributeUsage(AttributeTargets.Class), AllowMultiple = false, Inherited = false ]


Probably way beyond what you're actually asking the question for, but I've noticed that there is a slight (and probably inconsequential) difference between the two. Since I can't explain it myself I've started a new question about it here.

这篇关于什么时候使用WPF绑定中的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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