How can I get the Target Object and Target Property from a Binding

518 Views Asked by At

I will note that this is similar to but different than WPF: Get Target Object from derived Binding Class because I'd like to get this information from the basic Binding class.

My goal is to get the Target Object and Target Property of Binding given that Binding (if it was set).

I have tried to derive a class from Binding but unfortunately the method I seem to need to override, ProvideValue, is marked as sealed otherwise it would be as simple as:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget target)
    {
        _targetObject = target.TargetObject;
        _targetProperty = target.TargetProperty;
    }
    return base.ProvideValue(serviceProvider);
}

And this would probably only work for Bindings set in XAML. Likewise, the solution for the similar question probably also only works in XAML.

I have tried (probably unwisely) to derive from Binding and force override ProvideValue because I mostly only set Bindings from XAML anyway but I couldn't get it to work anyway.

I need the targets because I'm creating an attachable validation for the Bindings because the default Validation has many shortcomings but because Binding does not derive from DependencyObject I cant attach DependencyProperty so I can't get the targets via the standard avenue.

0

There are 0 best solutions below