使用 IDataErrorInfo 在验证期间启用禁用保存按钮
如何在使用 IDataErrorInfo 进行验证时禁用/启用按钮?
我正在使用 GalaSoft light Framework 来使用 MVVM。在我的模型类中,我实现了 IDataErrorInfo 来显示错误消息。
public string this[string columnName]
{
get
{
Result = null;
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
{
Result = "Please enter first name";
}
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
{
Result = "Please enter last name";
}
}
else if (columnName == "Address")
{
if (String.IsNullOrEmpty(Address))
{
Result = "Please enter Address";
}
}
else if (columnName == "City")
{
if (String.IsNullOrEmpty(City))
{
Result = "Please enter city";
}
}
else if (columnName == "State")
{
if (State == "Select")
{
Result = "Please select state";
}
}
else if (columnName == "Zip")
{
if (String.IsNullOrEmpty(Zip))
{
Result = "Please enter zip";
}
else if (Zip.Length < 6)
{
Result = "Zip's length has to be at least 6 digits!";
}
else
{
bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");
if (zipNumber == false)
{
Result = "Please enter only digits in zip";
}
}
}
else if (columnName == "IsValid")
{
Result = true.ToString();
}
return Result;
}
}
屏幕截图:https://i.sstatic.net/kwEI8.jpg
如何禁用/启用保存按钮。请建议?
谢谢
How to disable/enable a button while doing validation using IDataErrorInfo
?
I am using MVVM
using GalaSoft light Framework. In my Model class I have implemented IDataErrorInfo
to display the error messages.
public string this[string columnName]
{
get
{
Result = null;
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
{
Result = "Please enter first name";
}
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
{
Result = "Please enter last name";
}
}
else if (columnName == "Address")
{
if (String.IsNullOrEmpty(Address))
{
Result = "Please enter Address";
}
}
else if (columnName == "City")
{
if (String.IsNullOrEmpty(City))
{
Result = "Please enter city";
}
}
else if (columnName == "State")
{
if (State == "Select")
{
Result = "Please select state";
}
}
else if (columnName == "Zip")
{
if (String.IsNullOrEmpty(Zip))
{
Result = "Please enter zip";
}
else if (Zip.Length < 6)
{
Result = "Zip's length has to be at least 6 digits!";
}
else
{
bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");
if (zipNumber == false)
{
Result = "Please enter only digits in zip";
}
}
}
else if (columnName == "IsValid")
{
Result = true.ToString();
}
return Result;
}
}
Screenshot: https://i.sstatic.net/kwEI8.jpg
How to disable/enable save button. Kindly suggest?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Josh Smith 方法是在模型:
ViewModel 然后包含一个
CanSave
属性,用于读取模型上的IsValid
属性:最后,如果您使用的是
RelayCommand
,您可以设置命令的谓词CanSave
属性,视图将自动启用或禁用该按钮。在 ViewModel 中:在 View 中:
就是这样!
PS:如果你还没有读过约什·史密斯的文章,它将改变你的生活。
The Josh Smith Way of doing this is to create the following methods in the Model:
The ViewModel then contains a
CanSave
Property that reads theIsValid
property on the Model:Finally, if you are using
RelayCommand
, you can set the predicate of the command to theCanSave
property, and the View will automatically enable or disable the button. In the ViewModel:And in the View:
And that's it!
PS: If you haven't read Josh Smith's article yet, it will change your life.
您可以添加一个布尔属性 CanSave 并将其设置在验证方法的末尾。将按钮中的 IsEnabled 绑定到 IsValid。
像这样的事情:
you can add add a boolean property CanSave and set it at the end of your valiation method. Bind the IsEnabled from your button to IsValid.
Somthing like this:
这是我使用 IDataErrorInfo 接口、ValidationErrors Dictionary 和 MVVM-Light 消息传递系统组合的方法。简单明了,工作起来很有魅力:
模型类
查看隐藏代码
Here is my way of doing it using a combination of IDataErrorInfo interface, ValidationErrors Dictionary, and MVVM-Light messaging system. Straight forward and works like charm:
Model Class
View Code Behind