Assembly: PresentationFramework (in presentationframework.dll)
Windows Presentation Foundation (WPF) data binding provides a simple and consistent way for applications to present and interact with data. Data binding enables you to synchronize the values of the properties of two different objects.
To establish a binding, use the Binding class or one of the other classes that inherit BindingBase. Regardless of what object you are binding and the nature of your data source, each binding follows the model illustrated by the following figure.
The figure demonstrates the following fundamental WPF data binding concepts.
Each binding typically has these four components: a binding target object, a target property, a binding source, and a Path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.
The target property must be a dependency property. This also means that you cannot bind a field. Most properties of UIElement objects are dependency properties and most dependency properties, except read-only ones, support data binding by default. (Only DependencyObject types can define dependency properties and all UIElement objects derive from DependencyObject.)
Although not specified in the figure, it should be noted that the binding source object is not restricted to being a custom CLR object. WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.
Use the Mode property to specify the direction of the data flow. To detect source changes in one-way or two-way bindings, the source must implement a suitable property change notification mechanism such as INotifyPropertyChanged. For an example, see Implement Property Change Notification. The UpdateSourceTrigger property specifies the timing of source updates. For more information, see "Basic Data Binding Concepts" in Data Binding Overview.
This example shows you how to create a simple Binding.
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 Name="root" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:SDKSample" xmlns:system="clr-namespace:System;assembly=mscorlib" 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:
As a result, the TextBlock appears with the value "Joe".
For the complete sample, see Simple Binding Sample.
|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.
|How to: Create a Binding in Code|| |
This example shows how to create and set a Binding in code.
|How to: Bind to XML Data Using an XMLDataProvider and XPath Queries|| |
This example shows how to bind to XML data using an XmlDataProvider.
|How to: Implement Property Change Notification|| |
To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. This example shows how to create a class that implements INotifyPropertyChanged.
|How to: Specify the Direction of the Binding|| |
This example shows how to specify whether the binding updates only the binding target (target) property, the binding source (source) property, or both the target property and the source property.
|How to: Control When the TextBox Text Updates the Source|| |
This topic describes how to use the UpdateSourceTrigger property to control the timing of binding source updates. The topic uses the TextBox control as an example.
|How to: Bind to an ADO.NET Data Source|| |
This example shows how to bind a Windows Presentation Foundation (WPF)ListBox control to an ADO.NETDataSet.
|How to: Sort Data in View|| |
This example describes how to sort data in a view.
|How to: Filter Data in a View|| |
This example shows how to filter data in a view.