feature: Bind validation to non-string view properties

未关闭
#463 0 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp
领域
frontend

调研方向

从现有的 BindValidation 和 OneWayBind API 开始,使用提议的泛型签名以及 WhenAnyObservable 替代方案作为行为上下文。确定如何通过 selector 将验证状态映射到非 string 类型的 view 属性;当所请求的重载支持 Color-style binding,同时保留现有的验证行为时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

Is your feature request related to a problem? Please describe.

Not related to a problem.

Describe the solution you'd like

So what I'm trying to do is change the color of control in my view based on the validation state of a validation helper. The control does have an error property or something similar, and I do not want to show an inline error message anyway for this particular situation. What I would like to be able to do is something like this:

this.BindValidation(ViewModel, viewModel => viewModel.Property, view => view.Control.Color,
    validationState => validationState.IsValid ? Color.Green : Color.Red); 

This would similar to the overload of the OneWayBind function that allows you to bind between types like so:

this.OneWayBind(ViewModel, viewModel => vm.BoolProperty, view => view.Control.Color,
    boolProperty => boolProperty ? Color.Green : Color.Red);

Describe alternatives you've considered

Currently this is how I'm achieving the same effect:

this.WhenAnyObservable(v => v.ViewModel.PropertyValidator.ValidationChanged)
    .Select(validationState => validationState.IsValid ? Color.Green : Color.Red)
    .BindTo(this, v => v.Control.Color);

This works, and I don't necessarily hate it, but I would prefer the proposed form for brevity and to match with the rest of my simple bindings.

Describe suggestions on how to achieve the feature

I'm not sure exactly how to make this work or I probably just would have made done it and made PR, but I think the signatures of the overloads for such a change would look something like this:

public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty, TViewProperty>(
    this TView view,
    TViewModel? viewModel,
    Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
    Expression<Func<TView, TViewProperty>> viewProperty,
    Expression<Func<IValidationState, TViewProperty>> selector,
    IValidationTextFormatter<string>? formatter = null)
    where TView : IViewFor<TViewModel>
    where TViewModel : class, IReactiveObject, IValidatableViewModel

and this:

public static IDisposable BindValidation<TView, TViewModel, TViewProperty>(
    this TView view,
    TViewModel? viewModel,
    Expression<Func<TViewModel?, ValidationHelper?>> viewModelHelperProperty,
    Expression<Func<TView, TViewProperty>> viewProperty,
    Expression<Func<IValidationState, TViewProperty>> selector,
    IValidationTextFormatter<string>? formatter = null)
    where TView : IViewFor<TViewModel>
    where TViewModel : class, IReactiveObject, IValidatableViewModel

Additional context

主要语言
C#
星标
273
派生
30
平均合并
52 分钟
30 天内合并 PR
3

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

reactiveui/ReactiveUI.Validation 的其他 Issue

查看 reactiveui/ReactiveUI.Validation 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。