• Silverlight数据验证


    1.验证TextBox内容不超过指定长度,失去焦点后验证。

    前台:

    <TextBox Name="tb1" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True}" Height="100" Width="100"/>

    后台: 

    Person p = new Person();

    public MainPage()
    {
        InitializeComponent();
        p.Name = "123";
        tb1.DataContext = p;
    }
    public class Person : INotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (value.Length > 3)
                {
                    throw new Exception("不能超过三个字!");
                }
                name = value;
                NotifyChange("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
            }
        }
    }

    2.验证TextBox内容不超过指定长度,不失去焦点验证。只需要在上例的基础上为TextBox添加TextChanged事件,并且在事件里通知数据源即可。

     private void tb1_TextChanged(object sender, TextChangedEventArgs e)

    {
        System.Windows.Data.BindingExpression expresson = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
        expresson.UpdateSource();
    }

    对验证提示,可以进行样式的设置。

    前台添加NotifyOnValidationError属性和BindingValidationError事件

    <TextBox Name="tb1" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Height="100" Width="100" TextChanged="tb1_TextChanged" BindingValidationError="tb1_BindingValidationError"/> 

    后台实现BindingValidationError事件

    private void tb1_BindingValidationError(object sender, ValidationErrorEventArgs e)
    {
        if (e.Action == ValidationErrorEventAction.Added)
        {
            (sender as TextBox).Background = new SolidColorBrush(Colors.Red);

        }
        else if (e.Action == ValidationErrorEventAction.Removed)
        {
            (sender as TextBox).Background = new SolidColorBrush(Colors.White);
        }

    3.标注的方式验证。要添加System.ComponentModel.DataAnnotations.dll引用,并且将数据源的类定义修成为如下形式:

    public class Person : INotifyPropertyChanged
    {
        private string name;
        [StringLength(3, ErrorMessage = "不能超过3个字,这是标注的方式验证!")]
        public string Name
        {
            get { return name; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(thisnullnull) { MemberName = "Name" });
                name = value;
                NotifyChange("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
            }
        }

  • 相关阅读:
    理解AXI Quad Serial Peripheral Interface(SPI) IP核
    xilinx 高速收发器Serdes深入研究-Comma码(转)
    Zynq-PL中创建AXI Master接口IP及AXI4-Lite总线主从读写时序测试(转)
    一步一步开始FPGA逻辑设计
    万兆网调试(转)
    自定义AXI-IP核(转)
    在嵌入式设计中使用MicroBlaze(Vivado版本)(转)
    MicroBlaze核的串行接口实验:SPI UART
    Git超实用总结
    无法获取 vmci 驱动程序版本: 句柄无效
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/2668073.html
Copyright © 2020-2023  润新知