Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

xaml - When to use Path in WPF Binding?

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}"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...