.NET Zone is brought to you in partnership with:

Jonathan creates software, mostly with C#, XAML, and HTML5/JS. He was awarded the Microsoft MVP in the "Client Application Development section in January 2011 Jon is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

WPF 4.5 – Part 5 : The New BindingExpression Information

08.11.2012
| 3177 views |
  • submit to reddit

bindingExpressionBindingExpression is a useful API when working with Bindings from code. In WPF 4.0 it lacks some information which would have made it even mode helpful. Let’s discover what WPF 4.5 brought with him on this class.

For the record, this post is a part of a series about WPF 4.5 new features.

The MSDN page on WPF 4.5 new features sum-up very well what API has been added. So I will only explain a little more what it means and try to define in my own way the key terms.

What is a Binding?

Let’s tell that we have the Text property of a TextBlock binded to the Name Property of ViewModel.

<TextBlock x:Name="_textBlockWithBinding" Text="{Binding Name}" />

Then in this case you will have several elements defined by the Binding:

  1. The target is the TextBlock. This is the object on which the Binding is done : where the data goes;
  2. The target property is the DependencyProperty of the target object on which the binding is defined. Here it is the ‘Text’ Dependency property of the TextBlock;
  3. The source is the ViewModel : where the data comes from;
  4. The source property is the property aimed by Binding. Here it is the ‘Name’ property of the ViewModel. This element is retrieved from the Path property of the Binding;

Then we have to define the notion of ‘BindingGroup’ which is a way to create relationships between Bindings.
The goal is to be able to create ValidationRules for a group of properties instead of validating them one at a time.
You define a BindingGroup on a Panel and the controls inside of it are, excepting some case, a part of it. The Panel on which the BindingGroup is defined is known as the BindingGroup owner.

The BindingGroup class exposes then some method to manage the binded object state : BeginEdit, CommitEdit, CancelEdit. You can think of it as a way to perform the same feature as the IEditableObject.

Here is the MSDN example on how to define a BindingGroup from the XAML:

<StackPanel.BindingGroup>
    <BindingGroup NotifyOnValidationError="True">
      <BindingGroup.ValidationRules>
        <src:ValidateDateAndPrice ValidationStep="ConvertedProposedValue" />
      </BindingGroup.ValidationRules>
    </BindingGroup>
  </StackPanel.BindingGroup>

If you want more information on the BindingGroup, you can read this two posts:

  1. The BindingGroup page on MSDN, especially the ‘Remarks’ section;
  2. This blog post of Vincent Sibal;

How to retrieve the Binding Expression ?

The Binding expression is an object which contains information about a Binding.

To retrieve it, you have to know two things: the target property and the target object. Then you can call a static method from the BindingOperation class named ‘GetBindingExpression’ as in the following snippet:

BindingExpression bindingExpresion = BindingOperations
               .GetBindingExpression(_textBlockWithBinding, TextBlock.TextProperty);

Since WPF 4.5, the following property have been added to the BindingExpression class:

  1. Target: the target of the binding which is a DependencyObject;
  2. TargetProperty: the DependencyProperty targeted by the Binding;
  3. ResolvedSource: the object used as a source for the Binding; It can be null if it’s not found;
  4. ResolvedSourcePropertyName: the name of the source property used. It is null if the ResolvedSource is null. This is not the Path but only the property name;
  5. BindingGroup: the Binding group of the Binding if it exists;
  6. BindingGroup.Owner: the object that this BindingGroup, if it exists, is assigned to;
//The target
DependencyObject target = bindingExpresion.Target;
 
//The target property
DependencyProperty targetProperty = bindingExpresion.TargetProperty;
 
//The source object
object source = bindingExpresion.ResolvedSource;
 
//The source property name
string sourcePropertyName = bindingExpresion.ResolvedSourcePropertyName;
 
//The binding group
BindingGroup bindingGroup = bindingExpresion.BindingGroup;
 
//The binding group's owner
 if (bindingGroup != null)
 {
     DependencyObject bindingGroupOwner = bindingExpresion.BindingGroup.Owner;
 }
Published at DZone with permission of Jon Antoine, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)