Path:
Binding Path property is the path to the element property being used as the data source. Path is used to indicate from which property on the source object to get and set the bound data value. It is a property of type PropertyPath, which allows it to support a complex range of path expressions.
In the source code, we have a TextBox
which is bound to the Name
property of a Employee
object. Here’s an abridged version of the XAML I’m referring to:
Code Snippet
<TextBox Grid.Row="2" Text="{Binding EmployeeName}"></TextBox>
Notice that the binding statement in the XAML snippet above does not explicitly mention that it is setting the Binding
‘s Path
property. Markup extensions, such as Binding
, can have one property which acts as the "default" property when being set in XAML. Binding
‘s default property is Path
. If you are not in favor of using implicit notation like that, the XAML seen below is equivalent to the previous snippet:
Code Snippet
<TextBox Grid.Row="2" Text="{Binding Path=EmployeeName}"></TextBox>
Using Path=.
Observe the line 7 in the following code. In the employee class, I have two properties EmployeeName and State. So in the Grid DataContext, I am setting binding as Path=., which means the grid and all the controls in it can get access to all the properties in the DataContext. Line 19 and line20 uses the EmployeeName and line21 uses the state property.
Code Snippet
- <Window x:Class="Bindingtoclasses.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:src="clr-namespace:Bindingtoclasses"
-
- Title="MainWindow" Height="350" Width="525">
- <Grid DataContext="{Binding Path=.}">
- <Grid.Resources>
- <src:Employee x:Key="myDataSource" EmployeeName="Kishore1021"/>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBox Grid.Row="0" Text="{Binding Source={StaticResource myDataSource}, Path=EmployeeName, UpdateSourceTrigger=PropertyChanged}"/>
- <TextBox Grid.Row="1" Text="{Binding Source= {StaticResource myDataSource}, Path=EmployeeName}"></TextBox>
- <TextBox Grid.Row="3" Text="{Binding Path=EmployeeName, Mode=TwoWay}"></TextBox>
- <TextBox Grid.Row="2" Text="{Binding Path=EmployeeName, Mode=OneWayToSource}"></TextBox>
- <TextBlock Text="{Binding Path=State}"/>
-
- </Grid>
- </Window>
The Output of the program is as follows
Consider the following class where I have a class Employee that contains two properties EmployeeName and State. I have one more class called AnotherClass and have AnotherField as a property along with the EmployeeNameTest property which corresponds to the Employee class object. From line 23 to 28, we are creating an Employee object and initializing it with some values. And then created the AnotherClass object and used the previously created Employee object and assigned it to the property EmployeeNameTest. Finally, we are assigning the AnotherClass object c to this windows DataContext in line 28 in the following code.
The XAML for the above class to display the values is as follows. Points to notice from the XAML are
- In Line 7, we are using a period (.) path to bind to the current source which we set in the above code Line28 (this.DataContext = c).
- In Line 24, (Path=AnotherField); AnotherField is the name of the property of the source object to use for the binding. So the 5th textblock shows the AnotherField value when it binds.
- Line 21 & 22, (EmployeeNameTest.EmployeeName). This is concept of Subproperties. Subproperties of a property can be specified by a syntax in C# using ObjectName.PropertyName. For instance, the clause Path=EmployeeNameTest.EmployeeName sets the binding to the subproperty EmployeeName of the object or property EmployeeNameTest.
Code Snippet
- <Window x:Class="Bindingtoclasses.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:src="clr-namespace:Bindingtoclasses"
-
- Title="MainWindow" Height="350" Width="525">
- <Grid DataContext="{Binding Path=.}">
- <Grid.Resources>
- <src:Employee x:Key="myDataSource" EmployeeName="Kishore1021"/>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBox Grid.Row="0" Text="{Binding Source={StaticResource myDataSource}, Path=EmployeeName, UpdateSourceTrigger=PropertyChanged}"/>
- <TextBox Grid.Row="1" Text="{Binding Source= {StaticResource myDataSource}, Path=EmployeeName}"></TextBox>
- <TextBox Grid.Row="2" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
- <TextBox Grid.Row="3" Text="{Binding Path=EmployeeNameTest.EmployeeName}"></TextBox>
- <TextBlock Grid.Row="4" Text="{Binding Path=EmployeeNameTest.EmployeeName}"/>
- <TextBlock Grid.Row="5" Text="{Binding Path=AnotherField}"/>
-
- </Grid>
- </Window>
You can download the source code form here.
Observe the class Employee shown below. We have a property EmployeeName.
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.ComponentModel;
-
- namespace Bindingtoclasses
- {
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- Employee P = new Employee("Hello World");
- this.DataContext = P;
- }
- }
-
- // This class implements INotifyPropertyChanged to support one-way and two-way bindings
- // (such that the UI element updates when the source has been changed dynamically)
- public class Employee : INotifyPropertyChanged
- {
- private string name;
- // Declare the event
- public event PropertyChangedEventHandler PropertyChanged;
-
- public Employee()
- {
- }
-
- public Employee(string value)
- {
- this.name = value;
- }
-
- public string EmployeeName
- {
- get { return name; }
- set
- {
- name = value;
- // Call OnPropertyChanged whenever the property is updated
- OnPropertyChanged("EmployeeName");
- }
- }
-
- //// Create the OnPropertyChanged method to raise the event
- protected void OnPropertyChanged(string name)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(name));
- }
- }
- }
-
- }
XAML to display the EmployeeName is shown below.
Code Snippet
- <Window x:Class="Bindingtoclasses.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:src="clr-namespace:Bindingtoclasses"
- Title="MainWindow" Height="350" Width="525">
- <Grid DataContext="{Binding Path=EmployeeName}">
- <Grid.Resources>
- <src:Employee x:Key="myDataSource" EmployeeName="Kishore1021"/>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBox Grid.Row="0" Text="{Binding Source={StaticResource myDataSource}, Path=EmployeeName, UpdateSourceTrigger=PropertyChanged}"/>
- <TextBox Grid.Row="1" Text="{Binding Source= {StaticResource myDataSource}, Path=EmployeeName}"></TextBox>
- <TextBox Grid.Row="2" Text="{Binding Path=.}"></TextBox>
- <TextBlock Grid.Row="3" Text="{Binding}"></TextBlock>
-
- </Grid>
- </Window>
Points to learn from the above XAML code:
- Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}", so line 18 is equal to line 19 in terms of binding. As long as the binding already has a data context (for instance, the inherited data context coming from a parent element), and whatever item or collection being returned by that context is appropriate for binding without requiring further path modification, a binding declaration can have no clauses at all: {Binding}
You can download the source code from here.
MSDN: Use the Path property to specify the source value you want to bind to:
-
In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName.
-
Subproperties of a property can be specified by a similar syntax as in C#. For instance, the clause Path=ShoppingCart.Order sets the binding to the subproperty Order of the object or property ShoppingCart.
-
To bind to an attached property, place parentheses around the attached property. For example, to bind to the attached property DockPanel.Dock, the syntax is Path=(DockPanel.Dock).
-
Indexers of a property can be specified within square brackets following the property name where the indexer is applied. For instance, the clause Path=ShoppingCart[0] sets the binding to the index that corresponds to how your property’s internal indexing handles the literal string "0". Nested indexers are also supported.
-
Indexers and subproperties can be mixed in a Path clause; for example, Path=ShoppingCart.ShippingInfo[MailingAddress,Street].
-
Inside indexers you can have multiple indexer parameters separated by commas (,). The type of each parameter can be specified with parentheses. For example, you can have Path="[(sys:Int32)42,(sys:Int32)24]", where sys is mapped to the System namespace.
-
When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.
-
Property names and slashes can be combined to traverse properties that are collections. For example, Path=/Offices/ManagerName specifies the current item of the source collection, which contains an Offices property that is also a collection. Its current item is an object that contains a ManagerName property.
-
Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}".
Escaping Mechanism
-
Inside indexers ([ ]), the caret character (^) escapes the next character.
-
If you set Path in XAML, you also need to escape (using XML entities) certain characters that are special to the XML language definition:
-
Additionally, if you describe the entire binding in an attribute using the markup extension syntax, you need to escape (using backslash \) characters that are special to the WPF markup extension parser:
-
Backslash (\) is the escape character itself.
-
The equal sign (=) separates property name from property value.
-
Comma (,) separates properties.
-
The right curly brace (}) is the end of a markup extension.
Default Behaviors
The default behavior is as follows if not specified in the declaration.
-
A default converter is created that tries to do a type conversion between the binding source value and the binding target value. If a conversion cannot be made, the default converter returns null.
-
If you do not set ConverterCulture, the binding engine uses the Language property of the binding target object. In XAML, this defaults to "en-US" or inherits the value from the root element (or any element) of the page, if one has been explicitly set.
-
As long as the binding already has a data context (for instance, the inherited data context coming from a parent element), and whatever item or collection being returned by that context is appropriate for binding without requiring further path modification, a binding declaration can have no clauses at all: {Binding} This is often the way a binding is specified for data styling, where the binding acts upon a collection. For more information, see the "Entire Objects Used as a Binding Source" section in the Binding Sources Overview.
-
The default Mode varies between one-way and two-way depending on the dependency property that is being bound. You can always declare the binding mode explicitly to ensure that your binding has the desired behavior. In general, user-editable control properties, such as TextBox.Text and RangeBase.Value, default to two-way bindings, whereas most other properties default to one-way bindings.
-
The default UpdateSourceTrigger value varies between PropertyChanged and LostFocus depending on the bound dependency property as well. The default value for most dependency properties is PropertyChanged, while the TextBox.Text property has a default value of LostFocus.