I have following code in my WPF application and I'm trying to implement input validation.
Model:
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;
}
}
}
View:
<TextBox Text="{Binding Customer.Name, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
Problem: The solution is not working as expected. Nothing happens when type data in Textbox. I'm not sure weather I have followed the right steps.
Could any one help me?
UpdateSourceTrigger=PropertyChanged? - Mike Eason