How do you get the default binding mode of dependency properties?

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

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

I implemented a custom MarkupExtension and still got this in my code:

 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... } } 
+6
source share
1 answer

Use DependencyProperty.GetMetadata in DependencyObject . This will give you the value of PropertyMetadata , which will usually be an instance of FrameworkPropertyMetadata . If possible, switch to this type and check the value of BindsTwoWayByDefault .

For instance:

 var metadata = property.GetMetadata(target) as FrameworkPropertyMetadata; if (metadata != null) { var isTwoWay = metadata.BindsTwoWayByDefault; } 
+4
source

All Articles