如何处理 ViewModel 中的 Validation.Error 而不是背后的 View 代码? [英] How can I handle a Validation.Error in my ViewModel instead of my View's code behind?

查看:19
本文介绍了如何处理 ViewModel 中的 Validation.Error 而不是背后的 View 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 WPF 验证在 MVVM 模式中工作.

I'm trying to get WPF validation to work within the MVVM pattern.

在我的视图中,我可以验证这样的文本框,它由代码隐藏方法HandleError"处理,该方法工作正常:

In my View, I can validate a TextBox like this which gets handled by the code-behind method "HandleError", which works fine:

<TextBox Width="200"
         Validation.Error="HandleError">
    <TextBox.Text>
        <Binding Path="FirstName"
             NotifyOnValidationError="True"
             Mode="TwoWay">
            <Binding.ValidationRules>
                <validators:DataTypeLineIsValid/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

但是,我想通过 DelegateCommand 处理我的 ViewModel 中的验证,但是当我使用以下代码尝试时,我收到显式错误'{Binding HandleErrorCommand}' is not a valid event handler method名称.只有生成的或代码隐藏的类上的实例方法才有效."

However, I would like to handle the validation in my ViewModel via a DelegateCommand but when I try it with the following code, I get the explicit error "'{Binding HandleErrorCommand}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid."

是否有任何解决方法,以便我们可以在 MVVM 模式中处理验证?

Are there any workaround for this so that we can handle validations within a MVVM pattern?

查看:

<TextBox Width="200"
         Validation.Error="{Binding HandleErrorCommand}">
    <TextBox.Text>
        <Binding Path="FirstName"
             NotifyOnValidationError="True"
             Mode="TwoWay">
            <Binding.ValidationRules>
                <validators:DataTypeLineIsValid/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

视图模型:

#region DelegateCommand: HandleError
private DelegateCommand handleErrorCommand;

public ICommand HandleErrorCommand
{
    get
    {
        if (handleErrorCommand == null)
        {
            handleErrorCommand = new DelegateCommand(HandleError, CanHandleError);
        }
        return handleErrorCommand;
    }
}

private void HandleError()
{
    MessageBox.Show("in view model");
}

private bool CanHandleError()
{
    return true;
}
#endregion

推荐答案

我不知道这是否对您有帮助,但我会提供相同的.

I don't know if this will help you, but I'll offer it all the same.

另外,我使用的是 Silverlight,而不是 WPF.

Also, I'm using Silverlight, not WPF.

我没有在我的视图中指定任何验证,无论是在后面的代码中还是在 xaml 中.我的视图只有数据绑定到 ViewModel 上的属性.

I don't specify any validation in my Views, neither in the code behind nor the xaml. My View has only data bindings to properties on the ViewModel.

我所有的错误检查/验证都由 ViewModel 处理.当我遇到错误时,我设置了一个 ErrorMessage 属性,该属性也绑定到视图.ErrorMessage 文本块(在视图中)有一个值转换器,如果错误为 null 或为空,它将隐藏它.

All my error checking/validation is handled by the ViewModel. When I encounter an error, I set a ErrorMessage property, which is bound to the view as well. The ErrorMessage textblock (in the view) has a value converter which hides it if the error is null or empty.

采用这种方式可以轻松地对输入验证进行单元测试.

Doing things this way makes it easy to unit test input validation.

这篇关于如何处理 ViewModel 中的 Validation.Error 而不是背后的 View 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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