Register Login

 

Welcome to the Coach Factor blog. Here you will find all of our ideas on software development. Subscribe at  http://blog.nventive.net.

# Wednesday, November 26, 2008

After presenting at Microsoft TechDays the “BlackBelt WPF DataBinding Session”, we received a very interesting question from one of the attendees:

If there are validation rules associated to a binding declared in XAML and validation fails due to invalid data, the value in the target (“onscreen value”) will not be in sync with the value in the source (your business object).

In the end, the folllowing screen shot shows what we want :

image

  1. The ErrorTemplate is active, because a validation error occured;
  2. The tooltip shows the range of valid values and the invalid value that was entered;
  3. The textbox shows the currently binded value, and not the actual invalid value that the user entered.

By default, the text box would show the invalid value, and would be out of sync with the binded object.

This is what we came up with to change that default behavior: when the validator fails, invoke the binding mechanism as to update the target (onscreen value) with the current value in the source (business object).

Here is the recipe:

  1. Implement INotifyPropertyChanged on your business object.
  2. Create the IRaiseNotifyPropertyChanged:

        1     public interface IRaiseNotifyPropertyChanged

        2     {

        3         void Raise(string PropertyName);

        4     }

  3. Implement IRaiseNotifyPropertyChanged on your business object as follows:

        1         public void Raise(string PropertyName)

        2         {

        3             if (PropertyChanged != null)

        4             {

        5                 PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

        6             }

        7         }

  4. Add these two properties to the class that implements your validation rule (derives from ValidationRule):

        1         public string PropertyName { get; set; }

        2         public object Target { get; set; }

  5. Modify the Validate method like this:

        1             if ((age < Min) || (age > Max))

        2             {

        3                 if (Target is IRaiseNotifyPropertyChanged)

        4                 {

        5                     IRaiseNotifyPropertyChanged t = Target as IRaiseNotifyPropertyChanged;

        6                     t.Raise(PropertyName);

        7                 }

        8 

        9                 return new ValidationResult(false,

       10                   "Please enter an age in the range: " + Min + " - " + Max + ". You entered : " + value.ToString());

       11             }

       12             else

       13             {

       14                 return ValidationResult.ValidResult;

       15             }

  6. Use the validation rule like this in your XAML:
    <c:agerangerule target="{Binding}" propertyname="Age" max="130" min="21">

We hope that this will be useful in your WPF adventures !

Wednesday, November 26, 2008 10:37:00 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
.net - WPF
Comments are closed.
Search
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Statistics
Total Posts: 98
This Year: 28
This Month: 1
This Week: 1
Comments: 17
Sign In