zoukankan      html  css  js  c++  java
  • 关于Silverlight Input Data Validation <一>

    先说说基于INotifyPropertyChanged接口的数据验证。

    要点:

    1.NotifyOnValidationError和ValidatesOnExceptions在默认情况下是False的,所以,要想让验证失败的消息显示出来,请把它设置为True。

    2.请指定你所要验证地控件的DataContext。

    3.如果你想等验证通过时再做其他的事,用Validation.GetHasError()。

    以下是我的代码:

     (1)xaml部分:

    //对应上面的要点1
    <TextBox Text="{Binding Mode=TwoWay, Path=MyID, NotifyOnValidationError=True, ValidatesOnExceptions=True}" MaxLength="6" Name="txtMyID" Height="25"  Width="100" />
    

     (2)xaml.cs部分:

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
           InitializeComponent();
             
          //对应上面的要点2
          MyPageValidation mv = new MyPageValidation();
          txtMyID.DataContext = mv;
        }
             
        private void btnClick_Click(object sender, RoutedEventArgs e)
        {
          //对应上面的要点3
          if(!Validation.GetHasError(this.txtMyID))
          {
          }
        }
    }
    

    (3)MyPageValidation验证类部分:

    public class MyPageValidation : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _MyID;
        int resultId;
        public string MyID
        {
            get
            {
               return _MyID;
            }
            set
            {
              if (value.Length == 0)
              {
                  throw new InvalidDataException("Please enter ID!");
              }
              if (!Int32.TryParse(value, out resultId))
              {
                  throw new InvalidDataException("Please enter Integer!");
              }
    
              _MyID = value;
              //如果我注释掉下面这段代码,验证照样正常执行
              //所以,我还没弄清楚这段代码的作用
              //知道的请告诉我,谢谢
              NotifyPropertyChanged("MyID");
             }
         }
    
         private void NotifyPropertyChanged(String propertyName)
         {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
         }
    }
    

     (4)我的自定义Exception部分:

    public class InvalidDataException : Exception
    {
         public InvalidDataException(string msg) : base(msg)
         {
         }
    }
    
  • 相关阅读:
    三阶幻方
    夺冠概率
    2013年5月5号蓝桥杯画图
    回型嵌套
    Modelsim6.5g SE
    Modelsim存波形文件
    Modelsim仿真时用Hex格式显示数据的方法
    Modelsim仿真时的Debug命令:$display和$monitor
    Modelsim报错(一)
    【转】为什么有的LDO的输出输入必须用陶瓷电容 ,而有的却规定必须用钽电容?
  • 原文地址:https://www.cnblogs.com/bobliu/p/1957290.html
Copyright © 2011-2022 走看看