I'm having a WPF Form contains a Single Property "PersonName
". I wish to raise error on NULL OR EMPTY
. I got a solution from Data Annotations, I refereed the tutorial http://www.c-sharpcorner.com/uploadfile/20c06b/screen-validation-with-data-annotations-in-wpf/
My XAML is
<TextBox Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
<Button Content="Save" IsDefault="True" Command="{Binding SaveCommand}"
IsEnabled="{Binding }" Width="150" Height="40"/>
I can able to see the error only on onPropertyChange
event. If I directly hit the button without touching the TextBox
means, I can't able to see the Error. How could I trigger the same functionality on Button
Click.
My Requirement is PersonName Should not be NULL
or EMPTY
, If the Property is NULL
or EMPTY
, then I need to disabled the Button, based on IDataErrorInfo
not by Property.Length
Initial, we don't apply any validation. Once wrong data entry or without value hitting Button, then I need a validation.
Use IDataErrorInfo interface and inplement it as below like
public class ABC : IDataErrorInfo
{
private string _PersonName;
public string PersonName
{
get { return _PersonName; }
set
{
_PersonName = value;
OnPropertyChanged("PersonName");
}
}
public string Error
{
get { return string.Empty; }
}
public string this[string columnName]
{
get
{
if ("PersonName" == columnName)
{
if (String.IsNullOrEmpty(PersonName))
{
return "Your Error Message";
}
}
}
}
}
and also change the xaml as
<TextBox Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}" />