So, after some time I've tried to solve this issue again.
ReactiveUI's bindings don't support INotifyDataErrorInfo validation as is. Therefore, I'd have to bind validation errors manually after binding the value. This can be simply done like that:
public MainWindow() {
// some initialization code should be here.
this.WhenActivated(cleanUp => {
// binding ItemB's Name property to ItemBTextBox's Text property.
this.Bind(ViewModel, x => x.ItemB.Name, x => x.ItemBTextBox.Text)
.DisposeWith(cleanUp);
// binding ItemB's Name property's validation errrors to ItemBTextBox.
ViewModel.ItemB.WhenAnyPropertyChanged()
.StartWith(ViewModel.ItemB)
.Subscribe(itemB =>
{
if (!itemB.HasErrors)
{
ClearValidationErrors(ItemBTextBox);
return;
}
var errorForName = newEmployee
.GetErrors(nameof(newEmployee.Name))
.Cast<string>()
.FirstOrDefault();
if (string.IsNullOrEmpty(nameError))
{
ClearValidationErrors(ItemBTextBox);
return;
}
SetValidationError(ItemBTextBox, errorForName);
})
.DisposeWith(cleanUp);
});
}
However, the following question remains: how to make the WPF UI element (ItemBTextBox) display the error we set from code-behind? How the ClearValidationErrors() and SetValidationError() methods should be implemented? The only way to set validation error for UI element (so validation template would show it) I could find was the following code using WPF bindings:
Validation.ClearInvalid(ItemBTextBox.GetBindingExpression(TextBox.TextProperty));
Validation.MarkInvalid(
ItemBTextBox.GetBindingExpression(TextBox.TextProperty),
new ValidationError(new NotifyDataErrorValidationRule(), itemB, errorForName, null));
The problem is that the whole WPF validation mechanism is based on WPF bindings. ReactiveUI's bindings don't rely on those. The workaround would be to create a dummy WPF binding and use the code above to clear and set validation errors from code-behind.
ItemBTextBox.SetBinding(TextBox.TextProperty, new Binding("Non_existent_property.")
{ Mode = BindingMode.OneTime }); // invoke this in MainWindow constructor.
This approach works, but it is quite ugly by its nature (we have to use dummy WPF bindings to make it work, these dummy bindings obviously throw binding errors, etc.). If someone knows a way to use WPF's ValidationTemplates to show validation errors (which could be set from code-behind) for UI elements without WPF bindings, please let me know.
UPD: So I've figured out other way to manipulate WPF's Validation.Errors property. It relies on the reflection and the fact that the Validation class has inner static methods AddValidationError() and RemoveValidationError(). So I can declare new static class:
public static class ValidationHelper
{
private static readonly MethodInfo AddValidationErrorMethod =
typeof(Validation).GetMethod("AddValidationError", BindingFlags.NonPublic | BindingFlags.Static);
private static readonly MethodInfo RemoveValidationErrorMethod =
typeof(Validation).GetMethod("RemoveValidationError", BindingFlags.NonPublic | BindingFlags.Static);
public static void AddValidationError(
ValidationError validationError,
DependencyObject targetElement)
{
AddValidationErrorMethod
.Invoke(null, new object[] {validationError, targetElement, true});
}
public static void ClearValidationErrors(DependencyObject targetElement)
{
foreach (var error in Validation.GetErrors(targetElement).ToArray())
RemoveValidationErrorMethod
.Invoke(null, new object[] { error, targetElement, true });
}
}
and use it like this:
ValidationHelper.ClearValidationErrors(ItemBTextBox);
ValidationHelper.AddValidationError(new ValidationError(new NotifyDataErrorValidationRule(), itemB, errorForName, null),
ItemBTextBox);
It's far from perfect, but it works. And you don't need to use any dummy WPF bindings.
UPD2: this may be kind of less relevant to the initial question, but I'm also going to add my naive extension method for binding INotifyDataErrorInfo errors to WPF controls' ValidationTemplate to the answer in case anyone with the same problem needs a reference.
// just a helper method to extract property name from the expression.
private static string GetPropertyName<T, TProperty>(this Expression<Func<T, TProperty>> property)
where T : class
{
if (!(property.Body is MemberExpression member))
throw new ArgumentException("A method is provided instead of a property.");
if (!(member.Member is PropertyInfo propertyInfo))
throw new ArgumentException("A field is provided instead of a property");
return propertyInfo.Name;
}
public static IDisposable BindValidationError
<TView, TViewModel, TValidatableObject, TProperty>(
this TView view,
TViewModel viewModel,
Expression<Func<TViewModel, TValidatableObject>> objectToValidateName,
Expression<Func<TValidatableObject, TProperty>> propertyToValidate,
Func<TView, DependencyObject> uiElementDelegate)
where TViewModel : class
where TView : IViewFor<TViewModel>
where TValidatableObject : class, INotifyDataErrorInfo
{
string lastError = null;
var propertyToValidateName = propertyToValidate.GetPropertyName();
return viewModel.WhenAnyValue(objectToValidateName)
.StartWith(objectToValidateName.Compile().Invoke(viewModel))
.Do(objectToValidate =>
{
var uiElement = uiElementDelegate.Invoke(view);
if (objectToValidate == null)
{
ValidationHelper.ClearValidationErrors(uiElement);
return;
}
ValidateProperty(
objectToValidate,
propertyToValidateName,
uiElement,
ref lastError);
})
.Select(objectToValidate => objectToValidate != null
? Observable.FromEventPattern<DataErrorsChangedEventArgs>(objectToValidate,
nameof(objectToValidate.ErrorsChanged))
: Observable.Empty<EventPattern<DataErrorsChangedEventArgs>>())
.Switch()
.Subscribe(eventArgs =>
{
if (eventArgs.EventArgs.PropertyName != propertyToValidateName)
return;
var objectToValidate = (INotifyDataErrorInfo) eventArgs.Sender;
var uiElement = uiElementDelegate.Invoke(view);
ValidateProperty(
objectToValidate,
propertyToValidateName,
uiElement,
ref lastError);
});
}
Use it in the view's WhenActivated:
this.Bind(
ViewModel,
viewModel => viewModel.ItemB.Name,
view => view.ItemBTextBox.Text)
.DisposeWith(cleanUp);
this.BindValidationError(
ViewModel,
viewModel => viewModel.ItemB,
itemB => itemB.Name,
view => view.NewEmployeeNameTextBox)
.DisposeWith(cleanUp);