views:

18

answers:

1

I'm probably searching for this the wrong way, but:

is there any way to get the resulting value of a binding through code?

Probably something glaring obvious, but I just can't find it.

+1  A: 

You just need to call the ProvideValue method of the binding. The hard part is that you need to pass a valid IServiceProvider to the method... You can use the following trick:

class DummyDO : DependencyObject
{
    public object Value
    {
        get { return (object)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(object), typeof(DummyDO), new UIPropertyMetadata(null));

}

public object EvalBinding(Binding b)
{
    DummyDO d = new DummyDO();
    BindingOperations.SetBinding(d, DummyDO.ValueProperty, b);
    return d.Value;
}

...

Binding b = new Binding("Foo.Bar.Baz") { Source = dataContext };
object value = EvalBinding(b);

Not very elegant, but it works...

Thomas Levesque
Ah yes, that was it. Thanks.
Inferis