DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

WPF 4.5 – Part 5 : The New BindingExpression Information

Jon Antoine user avatar by
Jon Antoine
·
Aug. 12, 12 · Interview
Like (0)
Save
Tweet
Share
4.76K Views

Join the DZone community and get the full member experience.

Join For Free

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;
 }
Windows Presentation Foundation Binding (linguistics) Property (programming)

Published at DZone with permission of Jon Antoine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • Unlocking the Power of Polymorphism in JavaScript: A Deep Dive
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: