Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF : Define binding's default

Tags:

In WPF, I would like to be able to template how my bindings are applied by default.

For instance, I want to write :

Text="{Binding Path=PedigreeName}" 

But it would be as if I had typed :

Text="{Binding Path=PedigreeName, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"  

Any idea ?

Thanks,

  • Patrick
like image 485
PBelanger Avatar asked Jul 07 '09 19:07

PBelanger


People also ask

What is INotifyPropertyChanged in WPF?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is path binding WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

What is Updateourcetrigger WPF?

This is a property on a binding that controls the data flow from a target to a source and used for two-way databinding. The default mode is when the focus changes but there are many other options available, that we will see in this article.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.


2 Answers

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.

like image 168
Joe White Avatar answered Sep 19 '22 13:09

Joe White


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;     } } 
like image 29
Thomas Levesque Avatar answered Sep 18 '22 13:09

Thomas Levesque