Pregunta

I want to programmatically find out what the default binding mode of a property will be.

For example, if I check it against TextBox.TextProperty it should be BindingMode.TwoWay, but if it is ItemsControl.ItemsSourceProperty it should be BindingMode.OneWay.

I implemented a custom MarkupExtension and have gotten this far in my code so far:

public override object ProvideValue(IServiceProvider provider)
{
    var service = provider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

    if (service != null)
    {
        var target = service.TargetObject as DependencyObject;
        var property = service.TargetProperty as DependencyProperty;

        // Not sure what to do with the target and propery here...
    }
}
¿Fue útil?

Solución

Use DependencyProperty.GetMetadata on the DependencyObject. This will give you a PropertyMetadata value that will usually be an instance of FrameworkPropertyMetadata. Cast to that type if possible and check the value of BindsTwoWayByDefault.

For example:

var metadata = property.GetMetadata(target) as FrameworkPropertyMetadata;
if (metadata != null)
{
    var isTwoWay = metadata.BindsTwoWayByDefault;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top