문제

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...
    }
}
도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top