具有 DataAnnotation 的 ReactiveValidatedObject 强制重新验证 属性

ReactiveValidatedObject with DataAnnotation force revalidation of property

我试图验证 属性 仅当另一个 属性 设置为 true 时。我正在使用从另一个网页获取的 RequiredIfAttribute。

[RequiredIf("PropertyA",true)]
public string PropertyB
{
    get  
    {
        return _PropertyB;
    }
    set  
    {
        this.RaiseAndSetifChanged(x => x.PropertyB, value);
    }
}

RequiredIf 属性正在检查 PropertyA 是否设置为 true 然后进行验证,否则它会跳过验证并 return 成功。它的工作就像一个魅力,但问题是它只在 PropertyB 被更改时才起作用,但它不知道当 PropertyA 被更改时它需要刷新。所以我试图在 PropertyA 像这样更改时强制更新:

this.ObservableForProperty(x => x.PropertyA).Subscribe(obj =>
{
  this.RaisePropertyChanged(x=>x.PropertyB);
})

但它不起作用 - 没有任何反应。我认为它被忽略了,因为值没有改变。

还有另一种可行的方法,但与其说是解决方案,不如说是一种变通方法:

this.ObservableForProperty(x=>x.PropertyA).Subscribe(obj =>
{
 var temp = PropertyB;
 PropertyB = "anything"; //it forces revalidation
 PropertyB = temp; // it forces revalidation
})

希望 Binding Property with Property 对您有所帮助。