WPF - Getting a property value from a binding path

Viewed 18760

if I have an object say called MyObject, which has a property called MyChild, which itself has a property called Name. How can I get the value of that Name property if all I have is a binding path (i.e. "MyChild.Name"), and a reference to MyObject?

MyObject
  -MyChild
    -Name
4 Answers

I am doing it this way. Please let me know if this is a terrible idea, as C# is just a side job for me so I am not an expert objectToAddTo is of type ItemsControl:

BindingExpression itemsSourceExpression = GetaBindingExression(objectToAddTo);
object itemsSourceObject = (object)itemsSourceExpression.ResolvedSource;
string itemSourceProperty = itemsSourceExpression.ResolvedSourcePropertyName;

object propertyValue = itemsSourceObject.GetType().GetProperty(itemSourceProperty).GetGetMethod().Invoke(itemsSourceObject, null); // Get the value of the property
Related