views:

38

answers:

1

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}"
+6  A: 

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.

Martin Harris
So if I wanted to specify other parameters like Mode then I'd need to explicitly mark this as a Path?
Corey Ogburn
No, not if the path is your first argument: <TextBox Text="{Binding Width, ElementName=Button}"/> will work, <TextBox Text="{Binding ElementName=Button, Width}"/> will not, <TextBox Text="{Binding ElementName=Button, Path=Width}"/> will.
Martin Harris