使用WPF进行延迟输入验证

Vac*_*ano 3 validation wpf xaml .net-4.0

我的WPF应用程序中有以下场景:

  1. 用户在文本框中输入值.
  2. 我启动一个后台线程来验证该值(通过Web服务调用).
    (98%的时间输入有效.)
  3. 用户离开文本框并前进以处理下一个文本框.
  4. 后台线程返回,结果表明输入无效.
    它允许视图模型通过触发事件来了解这一点.(我正在使用Prism事件和模块.)

我需要一种方法来让该事件让TextBox知道在给出输入的情况下存在验证错误.(请记住,焦点不再是那个控件.)

我可以想到各种方法来"滚动我自己的验证"的东西并完成这项工作.但我想在WPF中的现有验证框架内工作.

注意:我不确定它是否相关,但我需要维护一个"NeededValidations"列表,在启用"保存"按钮之前必须通过这些列表.

Phi*_*hil 6

以下是使用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>
Run Code Online (Sandbox Code Playgroud)

查看模型

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
}
Run Code Online (Sandbox Code Playgroud)