none
WPF中多个数据验证只有最后一个的颜色能变正常,请问如何解决 RRS feed

  • 问题

  • 我在WPF中用数据绑定进行了三个文本框的数据绑定,并且在绑定的数据类里面设置了数据验证,通过IDataErrorInfo接口进行数据的验证,但是有一个问题就是当我第一个文本框的数据满足条件以后,边框颜色不会变正常,只有三个都满足条件以后组后一个释放焦点的文本框会变正常,其他两个还是红色的,但是所有验证条件确实满足了,我通过调试进行查看发现前两个都验证通过的时候后台的ValidationResults里面还有最后一个的错,只有都成功了ValidationResults才为空,此时最后一个文本框变成正常了,是否是因为这个原因导致前面两个输入框没法进行样式刷新?如何解决呢?
    2014年10月5日 13:41

答案

全部回复

  • Hi Clive,

    通过你的描述,我想你把3个文本框都绑定到同一个验证实例上面了,可以这样操作,分别建3个验证实例。另外:如果不能解决你的问题,建议提供一下你的代码。

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月7日 2:07
    版主
  • 非常感谢版主的回答。

    我是这样想的,但是我是用的一个实体类,里面对于每个属性进行了验证,通过属性上外挂特性的方式,我等下把代码贴出来。那么请问这样的情况应该如何解决呢?因为现在可能不太好分割这个类额。。。

    namespace MyFriends.Model
    {
        public class Friend : Validation.BaseValidationEntity<Friend>
        {
            public Friend()
            {
            }
    
            /// <summary>
            /// The <see cref="Name" /> property's name.
            /// </summary>
            public const string NamePropertyName = "Name";
    
            private string _name = string.Empty;
    
            /// <summary>
            /// Sets and gets the Name property.
            /// Changes to that property's value raise the PropertyChanged event. 
            /// </summary>
            [Required(ErrorMessage="Name must be set!")]
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    Set(() => Name, ref _name, value);
                }
            }
    
            /// <summary>
            /// The <see cref="Number" /> property's name.
            /// </summary>
            public const string NumberPropertyName = "Number";
    
            private string _number = string.Empty;
    
            /// <summary>
            /// Sets and gets the Number property.
            /// Changes to that property's value raise the PropertyChanged event. 
            /// </summary>
            [RegexValidator(@"^[1]+[3,5]+\d{9}$",ErrorMessage="Wrong MobileNumber Pattern!")]
            public string Number
            {
                get
                {
                    return _number;
                }
                set
                {
                    Set(() => Number, ref _number, value);
                }
            }
    
            /// <summary>
            /// The <see cref="Address" /> property's name.
            /// </summary>
            public const string AddressPropertyName = "Address";
    
            private string _address = string.Empty;
    
            /// <summary>
            /// Sets and gets the Address property.
            /// Changes to that property's value raise the PropertyChanged event. 
            /// </summary>
            public string Address
            {
                get
                {
                    return _address;
                }
                set
                {
                    Set(() => Address, ref _address, value);
                }
            }
    
            /// <summary>
            /// The <see cref="Email" /> property's name.
            /// </summary>
            public const string EmailPropertyName = "Email";
    
            private string _email = string.Empty;
    
            /// <summary>
            /// Sets and gets the Email property.
            /// Changes to that property's value raise the PropertyChanged event. 
            /// </summary>
            [RegexValidator(@"\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b",
                RegexOptions.IgnoreCase,
                ErrorMessage="Wrong Email Pattern!")]
            public string Email
            {
                get
                {
                    return _email;
                }
                set
                {
                    Set(() => Email, ref _email, value);
                }
            }
        }
    }
    

    2014年10月7日 3:17
  • 我改用DataAnnotation就可以了。。。用Microsoft.practice.enterpriselibrary的时候就会这样,请问是什么情况啊?这两个究竟如何取舍呢?
    2014年10月7日 7:27
  • Clive 你好,

    建议使用DataAnnotation,对Enterprise没有研究过,只知道这货用于企业级项目中比较多。

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月8日 2:41
    版主