C#そのものが初心者で、至らぬ点があるかと思いますがよろしくお願いいたします。
MVVMでGUI開発を行っております。
以下の条件の時のみ、ボタンを活性化するCommandを作成したいです。
- ReactiveProperty A,B,Cが何かしら入力されている
- ReactiveProperty A,Bは数字のみ
1.のコードは以下のように簡単に書けました。
C#
1 class MainViewModel : INotifyPropertyChanged 2 { 3 public ReactiveProperty<string> A { get; } = new ReactiveProperty<string>(string.Empty); 4 public ReactiveProperty<string> B { get; } = new ReactiveProperty<string>(string.Empty); 5 public ReactiveProperty<string> C { get; } = new ReactiveProperty<string>(string.Empty); 6 public AsyncReactiveCommand SendCommand { get; } 7 8 public MainViewModel() 9 { 10 SendCommand = new[] { A,B,C } 11 .CombineLatest(x => x.All(y => !string.IsNullOrEmpty(y))).ToAsyncReactiveCommand(); 12 SendCommand.Subscribe(async _ => { 13 ... 14 }); 15 }
プロパティAで数値以外が入っていたら以下のようにエラーを出すようにし、
C#
1 [RegularExpression("[0-9]+", ErrorMessage = "Num only")] 2 public ReactiveProperty<string> A { get; } = new ReactiveProperty<string>(string.Empty); 3 4 [RegularExpression("[0-9]+", ErrorMessage = "Num only")] 5 public ReactiveProperty<string> B { get; } = new ReactiveProperty<string>(string.Empty);
エラーがなかったら、処理するCommandを追加したいです。
C#
1 A.SetValidateAttribute(() => A); 2 B.SetValidateAttribute(() => B); 3 4 SendCommand = new[] { A.ObserveHasErrors, B.ObserveHasErrors } 5 .CombineLatest(x => x.All(y => !y)).ToAsyncReactiveCommand();
上手いこと、二つの条件をつなぎ合わせることはできないでしょうか。
完全に理解できていないので、上手いこと書けませんが何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー