在Silverlight中将零指定为文本框的默认值 [英] Assigining zero as default value to text box in silverlight

查看:58
本文介绍了在Silverlight中将零指定为文本框的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在从事Silverlight MVVM项目,

我有一个类似的要求,每当用户将文本框留为空白时,它都应默认为零.我还使用dataAnnotations对输入到textBox中的文本进行验证,如下所示

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;
     }
   }
}


将此属性绑定到UI中的TextBox.
仍然默认值不起作用.如果有人对此有任何想法,请告诉我.这对我会非常有帮助.预先感谢.


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.

推荐答案

")] [DefaultValue(" )] // 我也尝试了[DefaultValue(typeof(string),"0")]和公共字符串DEF_VAL ="0" [DefaultValue(DEF_VAL)],但是没有用. 公共 字符串天 { 获取 {返回 _天; } 设置 { Validator.ValidateProperty( ValidationContext(){MemberName = " 天"}); _days = ; } } }
")] [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; } } }


将此属性绑定到UI中的TextBox.
仍然默认值不起作用.如果有人对此有任何想法,请告诉我.这对我会非常有帮助.预先谢谢.


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.


为什么不能尝试这个

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";
     }
   }
}


这篇关于在Silverlight中将零指定为文本框的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆