WPF : Define binding's default WPF : Define binding's default wpf wpf

WPF : Define binding's default


Use one of the overloads of DependencyProperty.Register that take a PropertyMetadata. Pass an instance of FrameworkPropertyMetadata and set its properties.

public class Dog {    public static readonly DependencyProperty PedigreeNameProperty =        DependencyProperty.Register("PedigreeName", typeof(string), typeof(Dog),            new FrameworkPropertyMetadata() {                BindsTwoWayByDefault = true,                DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus            }        );

I don't offhand see a way to set the defaults for NotifyOnValidationError, ValidatesOnDataErrors, or ValidatesOnExceptions, but I haven't used this enough to be sure what to look for; they may be there.


In addition to Joe White's good answer, you could also create a class that inherits from Binding and sets the default property values you need. For instance :

public class TwoWayBinding : Binding{    public TwoWayBinding()    {        Initialize();    }    public TwoWayBinding(string path)      : base(path)    {        Initialize();    }    private void Initialize()    {        this.Mode = BindingMode.TwoWay;    }}