title: How to resolve bound object from bindingexpression with WPF?
body: <p>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?</p>
  
  <p>I'm attempting to write a Blend 3 behavior for textboxes which automatically invokes methods on an object bound to the textbox Text property.</p>
  
  <p>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.</p>
  
  <p>I first retrieve the binding expression from the behavior's associated object like so:</p>
  
  <pre><code>private BindingExpression GetTextBinding()
  {
      return this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>In addition, we have the relative path of the property which is bound on the binding expression's parent binding.</p>
  
  <p>So, we can get this information:</p>
  
  <pre><code>var bindingExpression = GetTextBinding();
  object dataContextItem = bindingExpression.DataItem;
  PropertyPath relativePropertyPath = bindingExpression.ParentBinding.Path;
  </code></pre>
  
  <p>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).</p>
  
  <p>Does anyone know where this might exist?  Any suggestions for alternative ways of resolving the bound object?</p>
  
  <p>Note, I'm trying to get at the bound object which is the <em>parent</em> 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.</p>
  
  <p>Thanks in advance for any help!
  Phil</p>