开发者

validation properties by attribute

开发者 https://www.devze.com 2022-12-24 13:06 出处:网络
I create class with two property - name,link(below). I use simple property validation by Required and StringLength attribute. I bind this class object to WPF ListBox(with textBoxs).

I create class with two property - name,link(below). I use simple property validation by Required and StringLength attribute. I bind this class object to WPF ListBox(with textBoxs).

But when I have textbox empty or write words longer than 8 sign nothing happens :/

What should I do to fires ErrorMessage? Or how to implement validation in other way ?

I also try use :

            if (value is int)
            {
                throw new ArgumentException("Wpisałeś stringa!!");
            }

But it only fires in debug mode :/

My class with implementation of attribute validation:

 public class RssInfo : INotifyPropertyChanged
{
    public RssInfo() { }
    public RssInfo(string _nazwa, string _link)
    {
        nazwa = _nazwa;
        link = _link;
    }

    private string nazwa;
    [Required(ErrorMessage = "To pole jest obowiązkowe nAZWA")]
    public string Nazwa
    {
        get { return nazwa; }
        set
        {
            if (value != nazwa)
            {
                nazwa = value;
                onPropertyChanged("Nazwa");
            }
            if (value is int)
            {
                throw new ArgumentException("Wpisałeś stringa!!");
            }
        }
    }
   开发者_StackOverflow中文版 private string link;
    [Required(ErrorMessage="To pole jest obowiązkowe link")]
    [StringLength(8, ErrorMessage = "Link cannot be longer than 8 characters")]
    public string Link
    {
        get { return link; }
        set
        {
            if (value != link)
            {
                link = value;
                onPropertyChanged("Link");
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void onPropertyChanged(string propertyName)
    {

        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


Is your TextBox TextProperty binded to your Name RSSInfo Property ? And is the binding mode TwoWays ? Also, remember that a Text binding in a TextBox is only updated when the TextBox loses keyboard focus. If you want to update the property on each keyboard input, use the UpdateSourceTrigger="PropertyChanged" attribute in the binding.

Show us your XAML too, the answer can be really simple ;-)

Also, I would suggest you to enhance your validation by using a custom ValidationRule on your TextBox. This will enable your UI to only present valid datas your model (RSSInfo) Name and Link properties, because a TextBox ValidationRule disables the Text binding when the user input is invalid. That's a must use ;-)

More information on ValidationRule here : http://weblogs.asp.net/monikadyrda/archive/2009/06/24/wpf-textbox-validation.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号