Silverlight数据验证
1.验证TextBox内容不超过指定长度,失去焦点后验证。前台:
<TextBox Name="tb1" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True}" Height="100" Width="100"/>
后台:
Person p = new Person();
public MainPage()
{
InitializeComponent();
p.Name = "123";
tb1.DataContext = p;
}
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if (value.Length > 3)
{
throw new Exception("不能超过三个字!");
}
name = value;
NotifyChange("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
页:
[1]