Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}" 
like image 579
Corey Ogburn Avatar asked Aug 17 '10 16:08

Corey Ogburn


People also ask

What are the different data binding modes available in WPF?

<TextBox x:Name="DestinationTextBox" Text="{Binding ElementName=SourceTextBox, Path=Text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Margin="{StaticResource MarginTop}" Width="150"

What is the use of INotifyPropertyChanged in WPF?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.

What is two way binding in WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.

What are the different binding mechanism in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.


1 Answers

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.

like image 115
Martin Harris Avatar answered Oct 14 '22 01:10

Martin Harris