8,756
社区成员




<Window x:Class="ErrorTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ErrorTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox HorizontalAlignment="Left"
Height="50" VerticalAlignment="Top" Width="237">
<TextBox.Text>
<Binding Path="FName" NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ValidClass></local:ValidClass>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window>
public class ValidClass : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var a = value.ToString();
if (string.IsNullOrWhiteSpace(a))
{
return new ValidationResult(false, "不可为空");
}
else
{
return new ValidationResult(true, null);
}
}
}
public class VM : INotifyPropertyChanged
{
private string _FName;
public string FName
{
get { return _FName; }
set { _FName = value; RaisePropertyChanged("FName"); }
}
private void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs (name));
}
public event PropertyChangedEventHandler PropertyChanged;
}