validation properties by attribute
Posted
by netmajor
on Stack Overflow
See other posts from Stack Overflow
or by netmajor
Published on 2010-03-26T00:01:28Z
Indexed on
2010/03/26
11:33 UTC
Read the original article
Hit count: 476
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("Wpisales 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 obowiazkowe nAZWA")]
public string Nazwa
{
get { return nazwa; }
set
{
if (value != nazwa)
{
nazwa = value;
onPropertyChanged("Nazwa");
}
if (value is int)
{
throw new ArgumentException("Wpisales stringa!!");
}
}
}
private string link;
[Required(ErrorMessage="To pole jest obowiazkowe 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));
}
}
}
© Stack Overflow or respective owner