How to resolve bound object from bindingexpression with WPF?

Viewed 17419

Hi does anyone know if there are any inbuilt classes for resolving a bound object from a bindingexpression and it's DataItem and property path?

I'm attempting to write a Blend 3 behavior for textboxes which automatically invokes methods on an object bound to the textbox Text property.

The textbox is bound to a property on a viewmodel class. What I want to do is resolve the viewmodel class from the binding expression and then make calls on this.

I first retrieve the binding expression from the behavior's associated object like so:

private BindingExpression GetTextBinding()
{
    return this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
}

Having done this, if we look at the binding expression, we can see it has a reference to the data context via the binding expression's DataItem property.

In addition, we have the relative path of the property which is bound on the binding expression's parent binding.

So, we can get this information:

var bindingExpression = GetTextBinding();
object dataContextItem = bindingExpression.DataItem;
PropertyPath relativePropertyPath = bindingExpression.ParentBinding.Path;

Now, this property path could potentially be a deeply nested and complex path, which I would very much like to avoid having to (re?)implement resolution of. I've searched around the .NET documentation and bounced around the assemblies with reflector, all to no avail - I can't find what surely must exist - there's got to be some class which performs the resolution of the path for the dataitem (the data context).

Does anyone know where this might exist? Any suggestions for alternative ways of resolving the bound object?

Note, I'm trying to get at the bound object which is the parent of the bound property (the string in this case) - I can obviously easily get at the bound value, but it's the parent I need.

Thanks in advance for any help! Phil

6 Answers

For people in the future who stumble on this question:

When .NET 4.5 becomes available it will have a number of new properties on the BindingExpression to greatly simplify what you are looking for.

ResolvedSource - The object that is actually being bound to, helpful when you have a binding source like 'grandparent.parent.me.Name'. This would return the 'me' object.

ResolvedSourcePropertyName - The name of the property on the ResolvedSource that is bound to. In the case above, "Name".

Similarly, there will be Target, and TargetName properties.

With these helper properties on BindingExpression you could use some shorter and much more simplified reflection that is more likely to work in extreme situations (indexers).

Just for further information, PropertyPath resolution is handled by an internal MS class called PropertyPathWorker, which lives in PresentationFramework under MS.Internal.Data. If you open it up in Reflector, you can see it's quite complicated, so I wouldn't recommend trying to duplicate its functionality. It's tightly coupled with the overall Binding architecture.

The most robust way to support all property path syntax--including attached dependency properties and hierarchical traversal--is probably to create a dummy DependencyObject with a DependencyProperty. You can then create a Binding from your 'owner' path to the dummy dependency property, create a new BindingExpression and then call the expression's UpdateTarget. It's a rather heavy way of accomplishing what, on the surface, looks like a simple task, but I think there are a lot of hidden gotchas in the way property paths are resolved for binding.

Just in case others find it useful, below is a quick one liner extension method for .net4.5 (and beyond) to retrieve the bound value..

public static object GetValue(this BindingExpression bindingExpression)
{
    return bindingExpression?.ResolvedSource.GetType().GetProperty(bindingExpression.ResolvedSourcePropertyName)?.GetValue(bindingExpression.ResolvedSource);
}
Related