Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/how-to-specify-the-binding-source.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

49 lines
4.3 KiB
Markdown

---
title: "How to: Specify the Binding Source"
description: Learn how to specify the binding source through this example in the Windows Presentation Foundation (WPF).
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "binding data [WPF], binding sources"
- "data binding [WPF], binding source"
- "binding sources [WPF]"
ms.assetid: 55d47757-2648-4a52-987f-b767953f168c
---
# 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.
## Example
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`.
[!code-xaml[DirectionalBinding#DataContext1](~/samples/snippets/csharp/VS_Snippets_Wpf/DirectionalBinding/CSharp/Page1.xaml#datacontext1)]
[!code-xaml[DirectionalBinding#DataContext2](~/samples/snippets/csharp/VS_Snippets_Wpf/DirectionalBinding/CSharp/Page1.xaml#datacontext2)]
The following example shows the definition of the `NetIncome` class.
[!code-csharp[DirectionalBinding#DataObject](~/samples/snippets/csharp/VS_Snippets_Wpf/DirectionalBinding/CSharp/billsdata.cs#dataobject)]
[!code-vb[DirectionalBinding#DataObject](~/samples/snippets/visualbasic/VS_Snippets_Wpf/DirectionalBinding/VisualBasic/NetIncome.vb#dataobject)]
> [!NOTE]
> 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 [Make Data Available for Binding in XAML](how-to-make-data-available-for-binding-in-xaml.md).
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|
|--------------|-----------------|
|<xref:System.Windows.Data.Binding.Source%2A>|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 <xref:System.Windows.Data.Binding.Source%2A> property instead of the `DataContext` property. For more information, see <xref:System.Windows.Data.Binding.Source%2A>.|
|<xref:System.Windows.Data.Binding.RelativeSource%2A>|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 <xref:System.Windows.Data.Binding.RelativeSource%2A>.|
|<xref:System.Windows.Data.Binding.ElementName%2A>|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 <xref:System.Windows.Controls.Slider> to control the height of another control in your application, or if you want to bind the <xref:System.Windows.Controls.ContentControl.Content%2A> of your control to the <xref:System.Windows.Controls.Primitives.Selector.SelectedValue%2A> property of your <xref:System.Windows.Controls.ListBox> control. For more information, see <xref:System.Windows.Data.Binding.ElementName%2A>.|
## See also
- <xref:System.Windows.FrameworkElement.DataContext%2A?displayProperty=nameWithType>
- <xref:System.Windows.FrameworkContentElement.DataContext%2A?displayProperty=nameWithType>
- [Property Value Inheritance](../advanced/property-value-inheritance.md)
- [Data Binding Overview](/dotnet/desktop-wpf/data/data-binding-overview)
- [Binding Declarations Overview](binding-declarations-overview.md)
- [How-to Topics](data-binding-how-to-topics.md)