WPF教程

WPF中DataErrorValidationRule验证规则html教程

时间:2013-6-20 1:14:00  作者:WPF之家  来源:http://www.wpf123.com  查看:227  评论:0
内容摘要:    ExceptionValidationRule验证规则可以根据目的属性到源属性值更改过程中所产生的异常来判断验证是否成功,但是在程序设计过程中程序设计人员可能并不希望产生异常,那么此时可以通过使实体类(数据对象)实现IDataErrorInfo接口,实现验证数据的目的,为...
  

  ExceptionValidationRule验证规则可以根据目的属性到源属性值更改过程中所产生的异常来判断验证是否成功,但是在程序设计过程中程序设计人员可能并不希望产生异常,那么此时可以通过使实体类(数据对象)实现IDataErrorInfo接口,实现验证数据的目的,为了让WPF知道数据对象实现了IDataErrorInfo接口,并且当修改了一个属性后,为了使用该接口检查错误,必须为Binding.ValidationRules集合添加DataErrorValidationRule验证规则。

  示例代码如下所示:

  窗体XAML代码如下所示:

<Window x:Class="WPF中DataErrorValidationRule验证规则.Window1"
    xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local=
"clr-namespace:WPF中DataErrorValidationRule验证规则"
    Title=
"Window1" Height="300" Width="300">
    <StackPanel Margin="10">
        <Button>确定</Button>
        <TextBox>
            <TextBox.Text>
                <Binding Source="{x:Static local:Window1.Stu}" Path="Age">
                    <Binding.ValidationRules>
                        <DataErrorValidationRule></DataErrorValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
            <!--定义验证错误模板-->
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Validation.ErrorTemplate">
                        <Setter.Value>
                            <ControlTemplate>
                                <DockPanel LastChildFill="True">
                                    <TextBlock Foreground="Red" FontWeight="Bold"
                                               DockPanel.Dock=
"Right" VerticalAlignment="Center">*</TextBlock>
                                    <Border
                                    BorderBrush=
"Red"
                                    BorderThickness=
"3"
                                    CornerRadius=
"3">
                                        <AdornedElementPlaceholder></AdornedElementPlaceholder><!--装饰占位符-->
                                    </Border>
                                </DockPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Style>
        </TextBox>
        <Button>确定</Button>
    </StackPanel>
</Window>

  窗体CS代码如下:

  private static InstanceClass.Stu stu =
            new WPF中DataErrorValidationRule验证规则.InstanceClass.Stu("dabiao",29);

        public static InstanceClass.Stu Stu
        {
            get 
            { 
                return stu;
            }
        }

  实体类代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WPF中DataErrorValidationRule验证规则.InstanceClass
{
    public class Stu:INotifyPropertyChanged,IDataErrorInfo
    {

        public Stu() { }
        public Stu(string names, int ages)
        {
            mynames = names;
            myages = ages;
        }

        private string mynames;
        private int myages;

        public string Name
        {
            get { return mynames; }
            set
            {
                mynames = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        public int Age
        {
            get { return myages; }
            set
            {
                myages = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Age"));
            }
        }

        #region IDataErrorInfo 成员

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get 
            {
                if (columnName=="Age")
                {
                    bool bl = true;
                    if (Age<0)
                    {
                        bl = false;
                    }
                    if (!bl)
                    {
                        return "输入了非数字";
                    }
                }
                return null;
            }
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged!=null)
            {
                PropertyChanged(this, e);
            }
        }

        #endregion
    }
}

本文来自2sws的博客,原文地址:http://hi.baidu.com/wangjunwangjuna/blog/item/35b7023304e28ee91b4cffeb.html


标签:content= 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号