私はModel
とPhoneModel
をViewModel
持っていPersonViewModel
ます。には、ビューに表示されているプロパティがあります。そこで、 に項目を追加および削除する をいくつか作成します。ただし、それらの追加、編集、および削除では、発火しません。そのコンテンツを強制的に検証するにはどうすればよいですか? 何か方法はありますか?INotifyPropertyChanged
IDataErrorInfo
PersonViewModel
ObservableCollection<PhoneModel> Phones
ItemsControl
Command
PhoneModel
Phones
Validation
ItemsControl
public class PhoneModel : INotifyPropertyChanged, IDataErrorInfo {
// has some validation roles and IDataErrorInfo is implemented full
public string Number { get; set; }
}
public class PersonViewModel : INotifyPropertyChanged, IDataErrorInfo {
public ObservableCollection<PhoneModel> Phones { get; set /* implements INotifyPropertyChanged */ ; }
}
そしてビューで:
<ItemsControl ItemsSource="{Binding Phones}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox
Text="{Binding Number,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,
ValidatesOnDataErrors=True}"
/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
助けてください。前もって感謝します。
更新 1
私は答えを試み@Michael G
ますが、それでもうまくいきません。私のコードはここにあります:
1) コレクションが変更されたイベント ハンドラーを追加します。
Phones.CollectionChanged += PhonesChanged;
2)そしてその中で私は試します:
void PhonesChanged(object sender, NotifyCollectionChangedEventArgs e) {
if(e.Action != NotifyCollectionChangedAction.Remove)
return;
var model = e.OldItems[0] as PhoneModel;
if(model != null) {
model.Validate();
OnPropertyChanged(() => Phones);
}
}
3) また、各電話アイテムのプロパティ変更をリッスンしようとします。
foreach(var phone in Phones)
phone.PropertyChanged += new PropertyChangedEventHandler(phone_PropertyChanged);
と:
void phone_PropertyChanged(object sender, PropertyChangedEventArgs e) {
var phone = sender as PhoneModel;
if(phone == null)
return;
phone.Validate();
OnPropertyChanged(() => Phones);
}