1

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?

  • The control definitely lights up with the orangered border then? What happens is you put the adornedelementplaceholder and error textblock in a dockpanel instead of grid? – Andy May 30 '19 at 15:16
  • Nothing appears, neither the bar is orangered nor message. – Fabrícia Santos May 31 '19 at 13:07

1 Answers1

1

Your problem is because IValidateObject ONLY returns those validation results.

WPF doesn't care about them.

You need to implement inotifydataerrorinfo or another interface that WPF understands and use those results in that.

You could adapt the code I use in this:

https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204

Take a look at the code in BaseEntity.

Andy
  • 11,864
  • 2
  • 17
  • 20