Binding to and validating a sub property in xaml
I have this binding in WPF xaml:
<DataGridTextColumn Binding="{Binding
Path=Patient.No,ValidatesOnDataErrors=True,NotifyOnValidationError=True}"/>
and I need to validate the binding on the DataGrid, but since the datagrid
is bound to the parent object, and the sub property is on the child
object, I don't know how to raise the property changed event from within
the child object's class to raise validation.
Here's the property code:
public PatientViewModel Patient
{
get
{
if (_p== null)
{
_p= new PatientViewModel(true);
_p.PropertyChanged+=new
PropertyChangedEventHandler(PatientPropertyChanged);
}
return _p;
}
}
void PatientPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//some code
RaisePropertyChanged("Patient.No");
}
//the 2nd level, PatientViewModel class
public int No
{
get
{
return _no;
}
set
{
if (_no != value)
{
_no= value;
if (_no == 0)
{
validationErrors.Add("No", "can't be zero");
}
else
{
if (validationErrors.ContainsKey("No") == true)
validationErrors.Remove("No");
}
RaisePropertyChanged("No");
}
}
}
Thinking it was the correct way to do it, I had to create the event
handler in the parent class, but none of those PropertyChanged events work
to raise validation.
So what is the correct syntax for that?
No comments:
Post a Comment