【发布时间】:2015-11-04 14:03:02
【问题描述】:
我的 WPF 应用程序中有以下代码,我正在尝试实现输入验证。
型号:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
视图模型:
public class CustomerViewModel : Screen, IDataErrorInfo
{
private Customer _customer;
public Customer Customer
{
get { return _customer; }
set
{
if (_customer != value)
{
_customer = value;
NotifyOfPropertyChange(() => Customer);
}
}
}
public string Error
{
get
{
throw new NotImplementedException();
}
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Customer.Name))
result = "Please enter a Name";
if (Customer.Name.Length < 3)
result = "Name is too short";
}
return result;
}
}
}
查看:
<TextBox Text="{Binding Customer.Name, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
问题: 该解决方案未按预期工作。在文本框中键入数据时没有任何反应。我不确定我是否采取了正确的步骤。
谁能帮帮我?
【问题讨论】:
-
试试
UpdateSourceTrigger=PropertyChanged怎么样?
标签: c# wpf mvvm caliburn.micro idataerrorinfo