Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
Updated 25-Sep-12 22:54pm
v2

1 solution

Why cant you try this

C#
{
     get{ return _days; }
     set
     {
       Validator.ValidateProperty(value, new ValidationContext(this, null, null){MemberName="Days"});
        if(value!="")
               _days=value;
        else
               _days="0";
     }
   }
}
 
Share this answer
 
Comments
krishnaprasad62 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900