我的WPF应用程序中有以下情形:
用户在文本框中输入一个值。
我启动一个后台线程去验证该值(通过Web服务调用)。
(输入有效时间的98%。)
用户离开文本框,然后继续处理下一个文本框。
后台线程返回,结果表明输入无效。
它通过触发事件让视图模型知道这一点。 (我正在使用Prism事件和模块。)
对于该事件,我需要一种方法让TextBox知道输入的内容存在验证错误。 (请记住,焦点不再位于该控件中。)
我可以想到各种各样的方法来“滚动我自己的验证”,并完成此工作。但是我想在WPF中的现有验证框架内工作。
注意:我不确定它是否相关,但是在启用“保存”按钮之前,我将需要维护一个必须全部通过的“ NeededValidations”列表。
最佳答案
您可以尝试使用IDataErrorInfo的几种方法。它们都依赖于以下事实:引发INotifyPropertyChanged通知将导致重新评估绑定验证。
有两个文本框。一个使用异步绑定,并假定Web服务调用是同步的。第二种使用同步绑定,并假定Web服务调用是异步的。
Xaml
<Window.DataContext>
<WpfValidationUpdate:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Margin="5" Grid.Row="0" Text="{Binding Text1, IsAsync=True, ValidatesOnDataErrors=True}"/>
<TextBox Margin="5" Grid.Row="1" Text="{Binding Text2, ValidatesOnDataErrors=True}"/>
</Grid>
查看模型
public class ViewModel : IDataErrorInfo, INotifyPropertyChanged
{
private string _text1;
private string _text2;
private bool _text1ValidationError;
private bool _text2ValidationError;
#region Implementation of IDataErrorInfo
public string this[string columnName]
{
get
{
if(columnName == "Text1" && _text1ValidationError)
{
return "error";
}
if(columnName == "Text2" && _text2ValidationError)
{
return "error";
}
return string.Empty;
}
}
public string Error
{
get
{
return string.Empty;
}
}
public string Text1
{
get { return _text1; }
set
{
_text1 = value;
OnPropertyChanged("Text1");
// Simulate web service synchronously taking a long time
// Relies on async binding
Thread.Sleep(2000);
_text1ValidationError = true;
OnPropertyChanged("Text1");
}
}
public string Text2
{
get { return _text2; }
set
{
_text2 = value;
OnPropertyChanged("Text2");
// Simulate web service asynchronously taking a long time
// Doesn't rely on async binding
Task.Factory.StartNew(ValidateText2);
}
}
private void ValidateText2()
{
Thread.Sleep(2000);
_text2ValidationError = true;
OnPropertyChanged("Text2");
}
#endregion
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}