延迟输入验证使用WPF [英] Delayed Input Validation with WPF

查看:196
本文介绍了延迟输入验证使用WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WPF应用程序存在以下情况:

I have the following scenario in my WPF application:

  1. 在用户输入在文本框中输入一个值。
  2. 在我启动一个后台线程去验证值(通过Web服务调用)。
    (98%的时间的输入是有效的。)
  3. 在用户离开对工作,对下一个文本框(S)文本框和头上。
  4. 后台线程返回,结果表明该输入是无效的。
    它可以让视图模型通过事件的触发知道这一点。 (我用的棱镜事件和模块。)
  1. The user enters a value in a text box.
  2. I start a background thread to go validate that value (Via a Web Service call).
    (98% of the time the input is valid.)
  3. The user leaves the text box and heads on to work on the next text box(s).
  4. The background thread returns and the results indicate that the input was invalid.
    It lets the view model know this via the firing of an event. (I am using Prism events and modules.)

我需要一种方式,该事件让文本框知道有一个验证错误与输入给了它。 (请记住,焦点不再在控制。)

I need a way for that event to let the TextBox know that there was a Validation error with the input it was given. (Remember that the focus is no longer in that control.)

我能想到的各种办法推出自己的验证的东西,得到这个工作的。不过,我想在WPF现有的验证框架内开展工作。

I can think of all kinds of ways to "roll my own validation" stuff and get this done. But I would like to work within an existing validation framework in WPF.

注:我不知道这是否是相关的,但我将需要维持NeededValidations,将所有必须通过之前,我能保存按钮列表

Note: I am not sure if it is relevant, but I will need to be maintaining a list of "NeededValidations" that will all have to pass before I enable the Save button.

推荐答案

下面是几个使用IDataErrorInfo方法,您可以试试。他们都依赖于一个事实,即提高INotifyPropertyChanged的通知将导致绑定验证重新评估。

Here are a couple of methods using IDataErrorInfo that you can try. They both rely on the fact that raising INotifyPropertyChanged notification will cause re-evaluation of binding validation.

有两个文本框。一种使用异步结合,并假定Web服务调用是同步的。第二个使用同步绑定,并假设Web服务调用是异步。

There are two text boxes. One uses an async binding and assumes the web service call is synchronous. The second uses a synchronous binding and assumes the web service call is async.

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
}

这篇关于延迟输入验证使用WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆