Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Silverlight
Hello everyone,
 
I am working on a silverlight MVVM project,
 
I am having a requirement like, whenever the user leaves a textbox as blank, it should default to zero. I am also performing validations to the text entered into textBox using dataAnnotations as follows
public class Data
{
   private string _days;
   [Range(0, 100)]
   [RegularExpression(@"^[0-7]*$")]
   [DefaultValue("0")] //I also tried [DefaultValue(typeof(string), "0")] and public string DEF_VAL="0" [DefaultValue(DEF_VAL)], but did not work.
   public string Days
   {
     get{ return _days; }
     set
     {
       Validator.ValidateProperty(value, new ValidationContext(this, null, null){MemberName="Days"});
_days=value;
     }
   }
}
Binded this property to TextBox in UI.
Still Default value is not working. Please let me know if anyone got any idea about this. It would be very helpful for me. Thanks in advance.
Posted 25-Sep-12 22:50pm
Edited 25-Sep-12 22:54pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why cant you try this
 
{
     get{ return _days; }
     set
     {
       Validator.ValidateProperty(value, new ValidationContext(this, null, null){MemberName="Days"});
        if(value!="")
               _days=value;
        else
               _days="0";
     }
   }
}
  Permalink  
Comments
krishnaprasad62 at 26-Sep-12 6:24am
   
Hi Santhosh, thanks for the reply. I have tried this one, but it is not working. Just to be clear with my query, i will explain it once again. If the text box is left empty, it should be filled with zero, when focus leaves that particular textbox. If i bind integer property, textbox will be automatically defaulted to zero, but here i am bib=nding a string property to TextBox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 495
1 OriginalGriff 339
2 ChintanShukla 305
3 Richard Deeming 260
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,768
1 OriginalGriff 7,387
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100