我该如何处理Validation.Error在我的视图模型,而不是背着我查看的code? [英] How can I handle a Validation.Error in my ViewModel instead of my View's code behind?

查看:152
本文介绍了我该如何处理Validation.Error在我的视图模型,而不是背着我查看的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让WPF验证,以MVVM模式中工作。

在我看来,我可以验证一个TextBox这样它获取由code隐藏方法的HandleError,它工作正常进行处理:

 <文本框宽度=200
         Validation.Error =的HandleError>
    < TextBox.Text>
        <绑定路径=姓
             NotifyOnValidationError =真
             模式=双向>
            < Binding.ValidationRules>
                <验证:DataTypeLineIsValid />
            < /Binding.ValidationRules>
        < /装订>
    < /TextBox.Text>
< /文本框>

不过,我想通过一个DelegateCommand来处理我的视图模型验证,但是当我用下面的code试试吧,我得到明确的错误'{结合HandleErrorCommand}不是一个有效的事件处理方法名。在生成或code-behind类唯一实例的方法是有效的。

有没有这方面的任何解决办法,使我们可以在MVVM模式中处理验证?

查看:

 <文本框宽度=200
         Validation.Error ={结合HandleErrorCommand}>
    < TextBox.Text>
        <绑定路径=姓
             NotifyOnValidationError =真
             模式=双向>
            < Binding.ValidationRules>
                <验证:DataTypeLineIsValid />
            < /Binding.ValidationRules>
        < /装订>
    < /TextBox.Text>
< /文本框>

视图模型:

 #地区DelegateCommand:的HandleError
私人DelegateCommand handleErrorCommand;公众的ICommand HandleErrorCommand
{
    得到
    {
        如果(handleErrorCommand == NULL)
        {
            handleErrorCommand =新DelegateCommand(的HandleError,CanHandleError);
        }
        返回handleErrorCommand;
    }
}私人无效的HandleError()
{
    MessageBox.Show(鉴于模式);
}私人布尔CanHandleError()
{
    返回true;
}
#endregion


解决方案

我不知道这是否会帮助你,但我会提供这一切一样。

另外,我使用Silverlight,WPF没有

我不我的看法指定任何验证,无论是在后面的code,也不是XAML。我的图只有数据绑定到视图模型属性。

我所有的错误检查/验证由视图模型处理。当我遇到一个错误,我设置了ErrorMessage属性,这势必会在视图中。该的ErrorMessage文本块(视图)的值转换器,它隐藏它,如果误差为null或空。

做事这种方式可以很容易地进行单元测试输入验证。

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

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>

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."

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

View:

<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>

ViewModel:

#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.

Also, I'm using Silverlight, not WPF.

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.

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.

这篇关于我该如何处理Validation.Error在我的视图模型,而不是背着我查看的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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