2

I have a problem using the IDataErrorInfo in combination with IReactiveBinding.Bind(). I hope someone here can help me.

I have a ViewModel that is inherited from ReactiveObject and implements the IDataErrorInfo interface.

public class MainWindowViewModel : ReactiveUI.ReactiveObject, IDataErrorInfo    
{

  private string username = string.Empty;

  public string Username
  {
    get { return this.username; }
    set { this.RaiseAndSetIfChanged(ref this.username, value); }
  }


  public MainWindowViewModel()
  {
    this.Validator = new MainWindowViewModelValidator();
  }

  public AbstractValidator<MainWindowViewModel> Validator { get; set; }

#region IDataErrorInfo Members

string IDataErrorInfo.Error
{
    get
    {
        return Validator != null ? string.Join(Environment.NewLine, Validator.Validate(this).Errors.Select(x => x.ErrorMessage).ToArray())
        : string.Empty;
    }
}

string IDataErrorInfo.this[string propertyName]
{
    get
    {
        if (Validator != null)
        {
            var results = Validator.Validate(this, propertyName);
            if (results != null
                && results.Errors.Count() > 0)
            {
                var errors = string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage).ToArray());
                return errors;
            }
        }
        return string.Empty;
    }
}

        #endregion
}

The MainWindowViewValidator ensures that the Usernameproperty is not empty.

The ViewModel is connected to the View in the code behind of the XAML-File:

public partial class MainWindow : IViewFor<MainWindowViewModel>
{

    public MainWindow()
    {
        InitializeComponent();

        this.ViewModel = new MainWindowViewModel();

        this.Bind(this.ViewModel, viewmodel => viewmodel.Username, view => view.Username.Text);
    }
        public MainWindowViewModel ViewModel
    {
        get { return (MainWindowViewModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel", typeof(MainWindowViewModel), typeof(MainWindow), new PropertyMetadata(null));

    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = (MainWindowViewModel)value; }
    }
}

The problem is now that the model validation is not called, as I don't specify the databinding in the XAML file directly.

Does anybody has a neat solution for this problem?

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
Rufus Buschart
  • 362
  • 1
  • 13

1 Answers1

1

The problem is now that the model validation is not called, as I don't specify the databinding in the XAML file directly.

ReactiveUI doesn't participate in IDataErrorInfo and friends for binding. It used to, but really if you think about it, Validation itself is a derived property of the form.

ReactiveUI is already really good at describing how properties are related to each other (via WhenAny/ToProperty), so you should just construct an ValidationError property that displays the error message.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209