Binding.Source Property
Gets or sets the object to use as the binding source.
Namespace: System.Windows.Data
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
By default, bindings inherit the data context specified by the DataContext property, if one has been set. However, the Source property is one of the ways you can explicitly set the source of a Binding and override the inherited data context. If you do not need the functionality of establishing a scope in which several properties inherit the same data context, you can use the Source property instead of the DataContext property.
The Binding.ElementName and Binding.RelativeSource properties also enable you to set the source of the binding explicitly. However, only one of the three properties, ElementName, Source, and RelativeSource, should be set for each binding, or a conflict can occur. This property throws an exception if there is a binding source conflict.
To clear this property, set it to DependencyProperty.UnsetValue.
In this example, you have a Person object with a string property named PersonName. The Person object is defined in the namespace called SDKSample.
The following example instantiates the Person object with a PersonName property value of Joe. This is done in the Resources section and assigned an x:Key.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:SDKSample" SizeToContent="WidthAndHeight" Title="Simple Data Binding Sample"> <Window.Resources> <src:Person x:Key="myDataSource" PersonName="Joe"/> ... </Window.Resources> ... </Window>
To bind to the PersonName property you would do the following:
<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
As a result, the TextBlock appears with the value "Joe".
In the following example, the Source values of the Binding objects are set to the static property Application.Current:
<ComboBox.IsEnabled> <MultiBinding Converter="{StaticResource specialFeaturesConverter}"> <Binding Path="CurrentUser.Rating" Source="{x:Static Application.Current}"/> <Binding Path="CurrentUser.MemberSince" Source="{x:Static Application.Current}"/> </MultiBinding> </ComboBox.IsEnabled>
For the full example, see Data Binding Demo.
More Code
| How to: Specify the Binding Source | In data binding, the binding source object refers to the object you obtain your data from. This topic describes the different ways of specifying the binding source. |
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.