Expand Minimize

How to: Specify the Binding Source

Updated: February 2010

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.

If you are binding several properties to a common source, you want to use the DataContext property, which provides a convenient way to establish a scope within which all data-bound properties inherit a common source.

In the following example, the data context is established on the root element of the application. This allows all child elements to inherit that data context. Data for the binding comes from a custom data class, NetIncome, referenced directly through a mapping and given the resource key of incomeDataSource.

For the complete code sample, see Controlling the Direction and Timing of the Data Flow Sample.

<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.DirectionalBinding"
  xmlns:c="clr-namespace:SDKSample"
  Name="Page1"
>
  <Grid.Resources>
    <c:NetIncome x:Key="incomeDataSource"/>
    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="Padding" Value="8"/>
    </Style>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Margin" Value="0,6,0,0"/>
    </Style>
  </Grid.Resources>
  <Grid.DataContext>
    <Binding Source="{StaticResource incomeDataSource}"/>
  </Grid.DataContext>


...


</Grid>

The following example shows the definition of the NetIncome class.

	public class NetIncome : INotifyPropertyChanged
	{
		private int totalIncome = 5000;
		private int rent = 2000;
		private int food = 0;
		private int misc = 0;
		private int savings = 0;
		public NetIncome()
		{
			savings = totalIncome - (rent+food+misc);
		}

		public int TotalIncome
		{
			get
			{
                return totalIncome;
			}
			set
			{
				if( TotalIncome != value)
				{
                    totalIncome = value;
					OnPropertyChanged("TotalIncome");
				}
			}
		}
		public int Rent
		{
			get
			{
				return rent;
			}
			set
			{
				if( Rent != value)
				{
					rent = value;
					OnPropertyChanged("Rent");
					UpdateSavings();
				}
			}
		}
		public int Food
		{
			get
			{
				return food;
			}
			set
			{
				if( Food != value)
				{
					food = value;
					OnPropertyChanged("Food");
					UpdateSavings();
				}
			}
		}
		public int Misc
		{
			get
			{
				return misc;
			}
			set
			{
				if( Misc != value)
				{
					misc = value;
					OnPropertyChanged("Misc");
					UpdateSavings();
				}
			}
		}
		public int Savings
		{
			get
			{
				return savings;
			}
			set
			{
				if( Savings != value)
				{
					savings = value;
					OnPropertyChanged("Savings");
					UpdateSavings();
				}
			}
		}

		private void UpdateSavings()
		{
			Savings = TotalIncome - (Rent+Misc+Food);
			if(Savings < 0)
			{}
			else if(Savings >= 0)
			{}
		}
		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(String info)
		{
            PropertyChangedEventHandler handler = PropertyChanged;
			if (handler !=null)
			{
				handler(this, new PropertyChangedEventArgs(info));
			}
		}
	}
NoteNote:

The above example instantiates the object in markup and uses it as a resource. If you want to bind to an object that has already been instantiated in code, you need to set the DataContext property programmatically. For an example, see How to: Make Data Available for Binding in XAML.

Alternatively, if you want to specify the source on your individual bindings explicitly, you have the following options. These take precedence over the inherited data context.

Property

Description

Source

You use this property to set the source to an instance of an object. 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. For more information, see Source.

RelativeSource

This is useful when you want to specify the source relative to where your binding target is. Some common scenarios where you may use this property is when you want to bind one property of your element to another property of the same element or if you are defining a binding in a style or a template. For more information, see RelativeSource.

ElementName

You specify a string that represents the element you want to bind to. This is useful when you want to bind to the property of another element on your application. For example, if you want to use a Slider to control the height of another control in your application, or if you want to bind the Content of your control to the SelectedValue property of your ListBox control. For more information, see ElementName.

Date

History

Reason

February 2010

Added example that defines the NetIncome class.

Content bug fix.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft