何时在 WPF 绑定中使用路径? [英] When to use Path in WPF Binding?

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

问题描述

我看过很多 WPF 绑定示例,并且在学习 MVVM 的过程中在很多不同的地方都使用过该功能,但是当您在绑定字符串中指定Path="时,我觉得有些不一致适合简单地输入要绑定的属性.例如,以下 XAML 属性之间的功能区别是什么:

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

推荐答案

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

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.

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

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天全站免登陆