Silverlight验证无效 [英] silverlight validations not working

查看:82
本文介绍了Silverlight验证无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是xaml代码

this is the xaml code

<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="203" Margin="0,5,0,0"

                 Text="{Binding UsrNm, Mode=TwoWay, ValidatesOnExceptions=True}" />


        <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="textBox2" VerticalAlignment="Top" Width="203" Margin="0,5,0,0" 

                 Text="{Binding UsrPwd, Mode=TwoWay, ValidatesOnExceptions=True}" />
        <Button Content="LogIn" Grid.Column="1" Grid.Row="2" Height="39" HorizontalAlignment="Left" Margin="112,27,0,0" Name="button1" VerticalAlignment="Top" Width="91" Click="button1_Click" />



这是mailpagexaml.cs中的.cs文件



and this is the .cs file in mailpagexaml.cs

public MainPage()
        {
            InitializeComponent();
            LoginInfo lgin = new LoginInfo();
            LayoutRoot.DataContext = lgin;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ele in LayoutRoot.Children)
            {
                //In our case check for text box
                if (ele is TextBox)
                {
                    //manually update source
                    (ele as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
                }

            }
        }

//and i took a class as logininfo.cs

 public class LoginInfo
    {
        private string _usrnm;
        private string _usrpwd;

        public string UsrNm 
        {
            get { return _usrnm; }
            set { _usrnm = value; } 
        }
        public string UsrPwd
        {
            get { return _usrpwd; }
            set { _usrpwd = value; } 
        }
    }


但是它不能用于验证...

我正在按照本教程
Silverlight 4:数据验证-每日技巧-部分1 [ ^ ]

请帮助我...


but it is not working for validation...

i am following this tutorial
Silverlight 4 : Data Validation - Tip of the Day - Part 1[^]

please help me...

推荐答案

您错过了实际的验证电话.使用ValidatesOnExceptions时,必须在属性设置器中引发异常,并且就验证而言,不会自动考虑属性.为了使其正常工作,您需要使用正确的参数调用System.ComponentModel.DataAnnotations.Validator.ValidateProperty.

但是,如果使用Silverlight 4,我建议您考虑使用IDataErrorInfo进行验证,因为我认为它提供了更多的灵活性.
You''re missing the actual validation call. With ValidatesOnExceptions, an exception must be thrown in the property setter, and the attributes are not taken into account automatically with respect to validation. For it to work you need a call to System.ComponentModel.DataAnnotations.Validator.ValidateProperty with the correct parameters.

However, if using Silverlight 4, I''d suggest looking into validating with IDataErrorInfo, as I feel it offers a lot more flexibility.


这篇关于Silverlight验证无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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