Now that you have looked at the INotifyDataErrorInfo
interface and at the inner workings of the custom validation system, let us examine how the ValidationSummary
control has been extended to support the INotifyDataErrorInfo
interface.
As you saw in the previous section, the ValidationSummary
control monitors its data context for validation errors if the data context implements INotifyDataErrorInfo
. When the INotifyDataErrorInfo.ErrorsChanged
event is raised by the data context, the control’s HandleErrorsChanged
handler is called (see Listing 26.5).
ValidationSummary
maintains a dictionary of validation errors, which are keyed by the associated property name. The HandleErrorsChanged
method retrieves the list of validation errors from the INotifyDataErrorInfo
and combines the list with the list in its own dictionary. If no list exists for the particular property name, a new list is added to the dictionary.
void HandleErrorsChanged(object sender, DataErrorsChangedEventArgs args)
{
INotifyDataErrorInfo notifier = sender as INotifyDataErrorInfo;
if (notifier == null)
{
return;
}
string propertyName = args.PropertyName;
if (string.IsNullOrEmpty(propertyName))
{
return;
}
IEnumerable notifierErrors = notifier.GetErrors(args.PropertyName);
List<object> errorList;
if (!errorLookup.TryGetValue(propertyName, out errorList))
{
errorList = new List<object>();
}
foreach (var error in errorList)
{
RemoveError(error);
}
errorList.Clear();
foreach (var error in notifierErrors)
{
AddError(error);
}
errorLookup[propertyName] = errorList;
foreach (var error in errorList)
{
AddError(error);
}
}
The ValidationSummary
control now has support for both the property setter validation approach and for the Silverlight 4 INotifyDataErrorInfo
validation approach.
18.218.9.139