Evaluating an Object’s PropertyPath

Evaluating an Object’s PropertyPath

PropertyPaths are a very useful binding concept, and can be useful in other instances as well. However, .NET 3.5 does not support evaluating PropertyPaths against objects directly without using binding. Here’s a workaround:

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)    {        Binding binding = new Binding(expression) { Source = container };        DependencyObject dummyDO = new DependencyObject();        BindingOperations.SetBinding(dummyDO, DummyProperty, binding);        return dummyDO.GetValue(DummyProperty);    }}

The following code provides a quick and easy way to test the workaround:

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;        }    }}
See also  Does It Make Sense to Splurge on a Laptop?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist