I would suggest two different ways of doing that:
1st way: ViewModel-side validation
If your ViewModel
implements IDataErrorInfo
to define errors in the inputs, then you must have a string Error
in your class.
This string is actually here for the exact thing you're looking to do: Having a single justification for multiple errors.
Here is my way of implementing it:
public string Error
{
get { return PerformValidation(string.Empty); }
}
public virtual string this[string propertyName]
{
get
{
return PerformValidation(propertyName);
}
}
My PerformValidation
method is virtual, to be overridden in the inheriting classes. But here is what it would look like:
protected override void PerformValidation(string propertyName = null)
{
if (string.IsNullOrEmpty(propertyName) || propertyName == "Property1")
{ }
else if (string.IsNullOrEmpty(propertyName) || propertyName == "Property2")
{ }
//...
}
Then, my TextBlock
is simply bound to the Error
string which would always show the first encountered error
2nd way: UI-ValidationRule side
Alternatively, you can simply use a MultiBinding
, or simply a TextBlock
containing Run
objects.
Simple example:
<TextBlock TextAlignment="Center">
<Run Text="{Binding ElementName=myElement,
Path=(Validation.Errors).CurrentItem,
Converter={StaticResource ErrorConverter}}" />
<Run Text="{Binding ElementName=myElement2,
Path=(Validation.Errors).CurrentItem,
Converter={StaticResource ErrorConverter}}" />
<Run Text="{Binding ElementName=myElement3,
Path=(Validation.Errors).CurrentItem,
Converter={StaticResource ErrorConverter}}" />
</TextBlock>
And just add one Run
per Element