I'm using IValidatableObject to validate my template in wpf. However, the error message is not returned to the interface. However, it is worth clarifying that validation works. The only problem is that the message is not displayed in the interface.
public string Host
{
get => _host;
set
{
_host = value;
OnPropertyChanged();
}
}
public string Port
{
get => _port;
set
{
_port = value;
OnPropertyChanged();
}
}
public string User
{
get => _user;
set
{
_user = value;
OnPropertyChanged();
}
}
public bool IsEmptyOrNull(string value) => string.IsNullOrWhiteSpace(value);
public IEnumerable<ValidationResult> Validate()
{
var results = new List<ValidationResult>();
var context = new ValidationContext(this);
Validator.TryValidateObject(this, context, results, true);
return results;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (IsEmptyOrNull(Host))
{
yield return new ValidationResult(Resources.EmptyOrNull, new[] { nameof(Host) });
}
if (IsEmptyOrNull(Port))
{
yield return new ValidationResult(Resources.EmptyOrNull, new[] { nameof(Port) });
}
if (IsEmptyOrNull(User))
{
yield return new ValidationResult(Resources.EmptyOrNull, new[] { nameof(User) });
}
if (IsEmptyOrNull(Password))
{
yield return new ValidationResult(Resources.EmptyOrNull, new[] { nameof(Password) });
}
if (IsEmptyOrNull(DataBase))
{
yield return new ValidationResult(Resources.EmptyOrNull, new[] { nameof(DataBase) });
}
}
Here interface XML
<TextBox x:Name="HostTextBox" Text="{Binding AppSetting.Host,UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" HorizontalAlignment="Left" Width="200" Margin="5,0,0,0" BorderBrush="#FF5774CB" mah:TextBoxHelper.Watermark="Localhost" TabIndex="1" FontSize="14"/>
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderBrush="OrangeRed" BorderThickness="2">
<Grid>
<AdornedElementPlaceholder/>
<TextBlock Text="{Binding [0].ErrorContent}" Foreground="OrangeRed" VerticalAlignment="Center" HorizontalAlignment="Right"
Margin="0,0,4,0"/>
</Grid>
</Border>
</ControlTemplate>
How do I display the error message on the interface from this validation?