将WPF ValidationRule的状态传递给MVVM中的视图模型 [英] Passing state of WPF ValidationRule to View Model in MVVM

查看:239
本文介绍了将WPF ValidationRule的状态传递给MVVM中的视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了一个看似普遍的要求.我有一个WPF Prism(用于MVVM)应用程序.我的模型实现了 IDataErrorInfo 进行验证. IDataErrorInfo 非常适合非数字属性.但是,对于数字属性,如果用户输入无效字符(非数字字符),则数据甚至都不会到达模型,因为wpf无法将其转换为数字类型.

I am stuck in a seemingly common requirement. I have a WPF Prism (for MVVM) application. My model implements the IDataErrorInfo for validation. The IDataErrorInfo works great for non-numeric properties. However, for numeric properties, if the user enters invalid characters (which are not numeric), then the data doesn't even reach the model because wpf cannot convert it to numeric type.

因此,我不得不使用WPF ValidationRule向用户提供一些有关无效数字条目的有意义的消息.视图中的所有按钮都绑定到棱镜的DelegateCommand(在视图模型中),并且按钮的启用/禁用是在视图模型本身中完成的.

So, I had to use WPF ValidationRule to provide user some meaningful message for invalid numeric entries. All the buttons in the view are bound to DelegateCommand of prism (in view model) and the enabling/disabling of buttons is done in View Model itself.

现在,如果某个文本框的wpf ValidationRule失败,我该如何将该信息传递给视图模型,以便它可以适当地禁用视图中的按钮?

Now if a wpf ValidationRule fail for some TextBox, how do I pass this information to View Model so that it can appropriately disable buttons in the view ?

推荐答案

Nirvan

解决此特定问题的最简单方法是使用数字文本框,该文本框可防止用户输入无效的值(您可以通过第三方供应商执行此操作,也可以找到一个开放的值源解决方案,例如从Textbox派生的用于禁止非数字输入的类.

The simplest way to solve this particular issue is to use a numeric textbox, which prevents the user from entering an invalid value (you can either do this via a Third Party Vendor, or find an open source solution, such as a class derived from Textbox that suppresses non numeric input).

无需执行上述操作即可在MVVM中处理此问题的第二种方法是在ViewModel中定义另一个字符串字段,并将该字段绑定到文本框.然后,在字符串字段的设置器中,可以设置Integer,并为数字字段分配一个值:

The second way to handle this in MVVM without doing the above, is to define another field in you ViewModel which is a string, and bind that field to your textbox. Then, in the setter of your string field you can set the Integer, and assign a value to your numeric field:

这是一个粗略的示例:(请注意,我没有对其进行测试,但是它应该可以为您提供想法)

Here is a rough example: (NOTE I did not test it, but it should give you the idea)

// original field
private int _age;
int Age 
{
   get { return _age; }
   set { 
     _age = value; 
     RaisePropertyChanged("Age");
   }
}


private string _ageStr;
string AgeStr
{
   get { return _ageStr; }
   set { 
     _ageStr = value; 
     RaisePropertyChanged("AgeStr");
     if (!String.IsNullOrEmpty(AgeStr) && IsNumeric(AgeStr) )
         Age = intVal;
    }
} 

private bool IsNumeric(string numStr)
{
   int intVal;
   return int.TryParse(AgeStr, out intVal);
}

#region IDataErrorInfo Members

    public string this[string columnName]
    {
        get
        {

            if (columnName == "AgeStr" && !IsNumeric(AgeStr)
               return "Age must be numeric";
        }
    }

    #endregion

这篇关于将WPF ValidationRule的状态传递给MVVM中的视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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