前提・実現したいこと
ReactivePropertyのSetValidateNotifyErrorで項目のチェック処理を行う際に
画面起動時必須エラーを表示させないようには以下の方法でできたのですが
https://blog.okazuki.jp/entry/2018/06/22/112213
一度画面で処理を行った後でTextBoxを初期化すると必須エラーが発生してしまいます
これを回避する方法はあるのでしょうか。
具体的には以下の通りです。
画面初回起動時は必須項目のFirstName、LastNameでエラーが発生していない
FirstNameを一度入力して削除した場合、エラーが発生する
FirstName,LastName入力後追加ボタンを押して、ListBoxに値追加
その後FirstName,LastNameの値をクリアすると必須エラーが発生し
赤枠表示になる
画面起動時と同様にこの赤枠表示を行わない、もしくは、赤枠表示をクリアしたい
よろしくおねがいします
XAML
1<Window 2 x:Class="BlankApp1.Views.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:prism="http://prismlibrary.com/" 6 Title="{Binding Title}" 7 Width="525" 8 Height="350" 9 prism:ViewModelLocator.AutoWireViewModel="True"> 10 <Grid> 11 <StackPanel> 12 <TextBlock Text="FirstName" /> 13 <TextBox Text="{Binding FirstName.Value, Mode=TwoWay}" /> 14 <TextBlock Text="LastName" /> 15 <TextBox Text="{Binding LastName.Value, Mode=TwoWay}" /> 16 <Button Command="{Binding CommandAdd}" Content="追加" /> 17 <ListBox ItemsSource="{Binding Items}" /> 18 </StackPanel> 19 </Grid> 20</Window>
ViewModel
1using Prism.Mvvm; 2using Reactive.Bindings; 3 4namespace BlankApp1.ViewModels 5{ 6 public class MainWindowViewModel : BindableBase 7 { 8 public ReactiveProperty<string> FirstName { get; set; } = new ReactiveProperty<string>(mode: ReactivePropertyMode.Default | ReactivePropertyMode.IgnoreInitialValidationError); 9 public ReactiveProperty<string> LastName { get; set; } = new ReactiveProperty<string>(mode: ReactivePropertyMode.Default | ReactivePropertyMode.IgnoreInitialValidationError); 10 public ReactiveCollection<string> Items { get; set; } = new ReactiveCollection<string>(); 11 public ReactiveCommand CommandAdd { get; set; } = new ReactiveCommand(); 12 13 public MainWindowViewModel() 14 { 15 this.FirstName.SetValidateNotifyError(z => string.IsNullOrEmpty(z) ? "nullです" : null); 16 this.LastName.SetValidateNotifyError(z => string.IsNullOrEmpty(z) ? "nullです" : null); 17 18 this.CommandAdd.Subscribe(() => 19 { 20 this.Items.Add(this.FirstName.Value + " " + this.LastName.Value); 21 this.FirstName.Value = string.Empty; 22 this.LastName.Value = string.Empty; 23 }); 24 } 25 } 26}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/10/30 09:11