We can register two properties to be kept in sync by the data binding engine using an instance of a Binding object, as shown in Example 6-7.

Example 6-7. Binding a UI target property to a data source property

<Binding Path="Age" /> </TextBox.Text>

In Example 6-7, we've used the property element syntax introduced in Chapter 1 to create an instance of the Binding markup extension class and initialize its Path property to Age. This establishes the synchronization relationship with the Text property of the TextBox object. Using the binding markup extension syntax (also introduced in Chapter 1), we can shorten Example 6-7 to the code snippet shown in Example 6-8.

Example 6-8. The shortcut binding syntax <TextBox Text="{Binding Path=Age}" />

As an even shorter cut, you can drop the Path designation altogether and the Binding will still know what you mean (see Example 6-9).

Example 6-9. The shortest cut binding syntax <TextBox Text="{Binding Age}" />

I prefer to be more explicit, so I won't use the syntax in Example 6-9, but I won't judge if you like it. As an example of something more exotic, Example 6-10 sets more than one attribute of a binding.

Example 6-10. A more full-featured binding example, longhand

<TextBox ... > <TextBox.Foreground> <Binding Path="Age" Mode="OneWay" Source="{StaticResource Tom}" Converter="{StaticResource ageConverter}" />

</TextBox.Foreground> </TextBox>

We'll see what all of these Binding properties mean directly. You might also be interested in how to pack multiple binding attribute settings using the shortcut syntax. To accomplish this, simply comma-delimit the name-value pairs, using spaces and newlines as convenient (see Example 6-11).

Example 6-11. A more full-featured binding example, shorthand <TextBox ...

Foreground="{Binding Path=Age, Mode=OneWay, Source={StaticResource Tom}, Converter={StaticResource ageConverter}}" />

Table 6-1 shows the list of available properties on a Binding object, many of which you'll see described in more detail later in this chapter.

Table 6-1. The Binding class's properties Property Meaning

BindsDirectlyToSource Defaults to False. If set to True, indicates a binding to the parameters of a

DataSourceProvider (like the ObjectDataProvider discussed later in this chapter), instead of to the data returned from the provider. See the BindingToMethod sample included with this book for an example.

Converter An implementation of IValueConverter to use to convert values back and forth from the data source. Discussed later in this chapter.

ConverterCulture Optional parameter passed to the IValueConverter methods indicating the culture to use during conversion.

ConverterParameter Optional application-specific parameter passed to the IValueConverter meth ods during conversion.

ElementName Used when the source of the data is a UI element as well as the target. Discussed later in this chapter.

Table 6-1. The Binding class's properties (continued)



IsAsync Mode

NotifyOnSourceUpdated NotifyOnTargetUpdated NotifyOnValidationError


RelativeSource Source



ValidationRules XPath


The value to use in case retrieving the value from the data source has failed, one of the parts of a multipart path is null, or the binding is asynchronous and the value hasn't yet been retrieved.

Defaults to False. When set to True, gets and sets the data on the source asynchronously. Uses the FallbackValue while the data is being retrieved.

One of the BindingMode values: TwoWay, OneWay, OneTime, OneWayToSource, or Default.

Defaults to False. Whether to raise the SourceUpdated event or not.

Defaults to False. Whether to raise theTargetUpdated event or not.

Defaults to False. Whether to raise the Validation.Error attached event or not. Discussed later in this chapter.

Path to the data of the data source object. Use the XPath property for XML data.

Used to navigate to the data source relative to the target. Discussed later in this chapter.

A reference to the data source to be used instead of the default data context.

Optional delegate to handle errors raised while updating the data source. Valid only if accompanied by an ErrorValidationRule (discussed later in this chapter).

Determines when the data source is updated from the UI target. Must be one of the

UpdateSourceTrigger values: PropertyChanged, LostFocus, Explicit, or Default. Discussed in Chapter 7.

Zero or more derivations of the ValidationRule class. Discussed later in this chapter.

XPath to the data on the XML data source object. Use the Path property for non-XML data. Discussed in Chapter 7.

The Binding class has all kinds of interesting facilities for managing the binding between two properties, but the one that you'll most often set is the Path property.* For most cases, you can think of the Path as the name of the property on the object serving as the data source. So, the binding statement in Example 6-8 is creating a binding between the Text property of the TextBox and the Name property of some object to be named later, as shown in Figure 6-8.

Figure 6-8. Binding targets and sources

Figure 6-8. Binding targets and sources

* Or XPath property, if your data is XML, which is discussed in Chapter 7.

In this binding, the TextBox control is the binding target, as it acts as a consumer of changes to the binding source, which is the object that provides the data. The binding target can be any WPF element, but you're only allowed to bind to the element's dependency properties (described in Chapter 1).

On the other hand, you can bind to any public CLR property or dependency property on the binding source object.* The binding source is not named in this example specifically so that we can have some freedom as to where it comes from at runtime and so that it's easier to bind multiple controls to the same object (like our name and age text box controls bound to the same Person object).

Commonly, the binding source data comes from a data context.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment