1

I have a WPF UserControl with a DependancyProperty that I would like to assign the default BindingMode of OneWayToSource.

So far, I've only found the FrameworkPropertyMetadataOptions.BindsTwoWayByDefault flag for the FrameworkPropertyMetadata, which lets me set OneWay or TwoWay binding as the default, but I can't find anything that allows me to set OneWayToSource as the default.

Current DependancyProperty registration:

public static readonly DependencyProperty ThumbDeltaProperty =
            DependencyProperty.Register("ThumbDelta", typeof(ThumbDeltaInfo), typeof(ThumbLink),
                                        new FrameworkPropertyMetadata(defaultValue: default(ThumbDeltaInfo),
                                                                      flags: FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

Is it possible to set OneWayToSource as the default BindingMode of a DependancyProperty in WPF?

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • i dont think so, these flags are wired pretty deep and defined in the PropertyMetadata class. you could create your own class that derives from Binding and use that in xaml: https://stackoverflow.com/a/35636247/6449190 – Milan Jan 13 '18 at 20:40
  • As you've seen already in the documentation, the framework's `Binding` class doesn't offer a mechanism to set the default to anything other than one-way or two-way. See first marked duplicate for work-around using your own binding class. See second marked duplicate for work-around using read-only target property, which in turn requires setting binding from code-behind, because the XAML compiler is annoying and doesn't allow any binding to read-only properties, even if the binding is one-way-to-source. There's also an attached-property solution there. – Peter Duniho Jan 13 '18 at 23:27
  • FWIW, I like the attached-property solution, because if it makes sense for the property to default to one-way-to-source, that property probably should be read-only, and the attached property is a convenient way to get around the XAML compiler's limitations. – Peter Duniho Jan 13 '18 at 23:28

0 Answers0