Repository navigation
ObservableValidator with subclasses #557
Answered
by
Sergio0694
himadrinath
asked this question in
Q&A
|
i have main view model also two classes in this viewmodel always false if i send FullName blank |
Answered by
Sergio0694
Jan 8, 2023
Replies: 2 comments 2 replies
|
You're trying to validate |
2 replies
Answer selected by
Sergio0694
|
@Sergio0694 How to implement it? I tried to write a public class ForwardErrorsAttribute : ValidationAttribute
{
public override bool IsValid(object? value)
{
return !((INotifyDataErrorInfo)value).HasErrors;
}
}But the code will not be executed. Update Edit: I found a solution: [ForwardErrors]
public SubclassProperty SubclassProperty
{
get => _subclassProperty;
set
{
if (EqualityComparer<SubclassProperty>.Default.Equals(_subclassProperty, value))
{
return;
}
OnPropertyChanging();
// Unregister oldValue events。
_subclassProperty.PropertyChanging -= SubclassPropertyChanging;
_subclassProperty.PropertyChanged -= SubclassPropertyChanged;
_subclassProperty.ErrorsChanged -= SubclassProperty_ErrorsChanged;
// Ensure newValue register events。
value.ErrorsChanged -= SubclassProperty_ErrorsChanged;
value.ErrorsChanged += SubclassProperty_ErrorsChanged;
value.PropertyChanging -= SubclassPropertyChanging;
value.PropertyChanging += SubclassPropertyChanging;
value.PropertyChanged -= SubclassPropertyChanged;
value.PropertyChanged += SubclassPropertyChanged;
_subclassProperty= value;
OnPropertyChanged();
}
}
private void SubclassProperty_ErrorsChanged(object? sender, DataErrorsChangedEventArgs e)
{
ValidateProperty(SubclassProperty, nameof(SubclassProperty));
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're trying to validate
TestViewModel, but this type has no validatable properties. Validation doesn't automatically handle nested properties, you'll have to either call the validation on both nested properties manually and forward the errors, or write some custom attribute that will implement the validation for a nested viewmodel. The fact you're currently not seeing errors is expected 🙂