如何编写具有验证能力的简单silverlight页面?

时间:2011-07-20 11:10:01

标签: silverlight

我需要用3个textBox编写简单的页面          1. textBox1 ==>仅包含数字          2. textBox2 ==>包含电子邮件地址          3. textBox3 ==>仅包含用户名

我想对每个textBox进行一些验证检查 - 如果用户输入有问题 - 要使textBox边框为红色并显示有关错误的正确消息。

我该怎么办?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

以下是一个示例:SimpleValidation

enter image description here

它使用DataAnnotations,因此您可以在属性中设置验证。

以下是代码:

MainPage.cs

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DataContext = new DataModel();
        }
    }

MainPage.xaml中

<UserControl x:Class="Org.Vanderbiest.SimpleValidation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="150" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="Number Textbox" />
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Email Textbox" />
        <TextBlock Grid.Row="2" Grid.Column="0" Text="Username Textbox" />

        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Number,Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Email,Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Username,Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
    </Grid>
</UserControl>

DataModel.cs:

public class DataModel : INotifyPropertyChanged
    {
        private string _number;
        [Range(0 ,1000, ErrorMessage = "Number must be between 0 and 1000")]
        public string Number
        {
            get { return _number; }
            set {
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Number" });
                _number = value;
                RaisePropertyChanged("Number");
            }
        }


        private string _email;

        [RegularExpression("[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}", ErrorMessage = "Invalid Email")]
        public string Email
        {
            get { return _email; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Email" });
                _email = value;
                RaisePropertyChanged("Email");
            }
        }

        private string _username;

        [Required(ErrorMessage = "User Name is Required")]
        [StringLength(12, MinimumLength = 6, ErrorMessage = "User Name must be in between 6 to 12 Characters")]
        public string Username
        {
            get { return _username; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "UserName" });
                _username = value;
                RaisePropertyChanged("Username");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }


    }

HTH

答案 1 :(得分:1)

如果你需要更强大/更复杂的验证,你可以使用数据注释,你可以尝试IDataErrorInfo MVVM方法,详见:http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages/