Поскольку данная ссылка на ответ в настоящее время доступна только в веб-архиве, я продублировал ответ, который был там дан:
public static class DataBinder
{
private static readonly DependencyProperty DummyProperty = DependencyProperty.RegisterAttached(
"Dummy",
typeof(Object),
typeof(DependencyObject),
new UIPropertyMetadata(null));
public static object Eval(object container, string expression)
{
var binding = new Binding(expression) { Source = container };
return binding.Eval();
}
public static object Eval(this Binding binding, DependencyObject dependencyObject = null)
{
dependencyObject = dependencyObject ?? new DependencyObject();
BindingOperations.SetBinding(dependencyObject, DummyProperty, binding);
return dependencyObject.GetValue(DummyProperty);
}
}
Пример:
public partial class PropertyPathParserDemo : Window
{
public PropertyPathParserDemo()
{
InitializeComponent();
Foo foo = new Foo() { Bar = new Bar() { Value = "Value" } };
this.Content = DataBinder.Eval(foo, "Bar.Value");
}
public class Foo
{
public Bar Bar
{
get;
set;
}
}
public class Bar
{
public string Value
{
get;
set;
}
}
}