RadControls for WPF

WPF data binding provides a simple and consistent way for applications to present and interact with data. Elements can be bound to data from a variety of data sources in the form of CLR objects and XML. ItemsControls such as ListBox have built-in functionality to enable flexible styling of single data items or collections of data items. Sort, filter, and group views can be generated on top of the data.

This topic contains the following sections.

What is Data Binding?

Data binding is the process that establishes a connection between the application UI and the business logic. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect the changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change.

Creating a Binding

This section contains the following subsections.

When you establish a binding, you use the Binding object, and each binding usually has four components: binding target, target property, binding source, and a path to the source value to use. This section discusses how to set up a binding.

Consider the following example, in which the binding source object is a class named MyData. The MyData class has a string property named Color and with its help the value is set to "Red".

CopyXAML
<UserControl.Resources>
    <example:MyData x:Key="DataSource"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource DataSource}}">   
    <TextBox Text="{Binding Color}"/>
</Grid>
Tip
You can read more information about the binding declaration syntax here.

Specifying the Binding Source

Notice that in the previous example, the binding source is specified by setting the DataContext property on the Grid element. The TextBox then inherits the DataContext value from the Grid, which is its parent element. To reiterate, the binding source object is one of the four necessary components of a binding. Therefore, without the binding source object being specified, the binding won't do anything.

There are several ways to specify the binding source object. One way is by using the DataContext property on a parent element - this is useful when you are binding multiple properties to the same source. However, sometimes it may be more appropriate to specify the binding source on individual binding declarations. For the previous example, instead of using the DataContext property, you can specify the binding source by setting the Source property directly on the binding declaration of the textbox. See the following example.

CopyXAML
<TextBox Text="{Binding Source={StaticResource DataSource}, Path=Color}"/>

Another way to specify the binding source object is to use the ElementName property or the RelativeSource property to specify the binding source. The ElementName property is useful when you are binding to other elements in your application. For example, when you are using a Slider to adjust the width of a Button. The RelativeSource property is useful when the binding is specified in a ControlTemplate.

Tip

There are several ways to specify the binding source object:

  • Using DataContext.
  • Explicitly specifying the binding source by setting the Binding's Source property.
  • Using ElementName.
  • Using RelativeSource.

Specifying the Path to the Value

If your binding source is an object, you use the Path property to specify the value which will be needed for your binding. If you are binding to XML data, you use the XPath property to specify the value.

Note that although the Path to the value to use is one of the four necessary components of a binding, in the scenarios when you want to bind to an entire object, the value to use would be the same as the binding source object. In those cases, it is applicable to not specify a Path, like in the example below.

CopyXAML
<ListBox ItemsSource="{Binding}"/>

In the above example the empty binding syntax is used ({Binding}). In this case, the ListBox inherits the DataContext from a parent Grid element (not shown in this example). When the path is not specified, the default is to bind to the entire object. Or in other words, the path has been left out because the ItemsSource is bound to the entire object.

Direction of the Data Flow

The data flow of a binding can go from the binding target to the binding source and/or from the binding source to the binding target if the binding source provides the proper notifications.

You may want your application to enable users to change the data and propagate it back to the source object. Or you may not want to enable users to update the source data. You can control this by setting the Binding's Mode property object. The following list illustrates the different types of data flow:

  • OneWay binding - makes changes to the source property to automatically update the target property, but changes to the target property are not propagated back to the source property. This type of binding is appropriate if the control being bound is implicitly read-only.
Note

In order to implement OneWay binding your business objects/collections must implement the INotifyPropertyChanged/INotifyCollectionChanged interfaces.

  • TwoWay binding - makes changes to either the source property or the target property to automatically update the other. This type of binding is appropriate for editable forms or other fully-interactive UI scenarios.
Note

In order to implement TwoWay binding your business objects/collections must implement the INotifyPropertyChanged/INotifyCollectionChanged interfaces.

  • OneTime binding - makes the source property to initialize the target property, but subsequent changes do not propagate.
  • OneWayToSource binding - updates the source property when the target property changes. This is the opposite to the OneWay binding.

See Also