У меня есть следующий код в моем приложении WPF, и я пытаюсь выполнить проверку ввода.WPF/Caliburn.Micro - Входная проверка с использованием IDataErrorInfo
Модель:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
ViewModel:
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»? –