質問

I have recently been trying to get into the habit of using MVVM design patterns within the project im working on with the aim of decoupling the various modules in the app and make it suitable for unit testing.

I have moved onto input validation. I am implementing IDataErrorInfo in my ViewModel...this seems to work pretty well for TextBox.

I'm currently trying to work out how to implement IDataErrorInfo on the ListBox control which is bound to an ObservableCollection of custom objects. Problem is each row (ListBoxItem) updates the model directly so my IDataErrorInfo implementation in the ViewModel is bypassed. So the way i see it i can do one of two things -

1) Create a secondary ViewModel (to represent ListBoxItems) which i use to populate the Observable Collection and implement IDataErrorInfo on that.

2) Implement IDataErrorInfo on my Model. However, since the ViewModel is mainly used to relay references from my Model i would then probably consider moving all my IDataErrorInfo implementation to my Model class for consistency...

What would you do?

Also...any advice on creating reusable validation methods which i can easily apply to any type of Control?

Cheers

役に立ちましたか?

解決

What would you do?

The first option, of course - create a secondary view model for list items. You shouldn't implement IDataErrorInfo in your models, because this is UI-specific functional.

reusable validation methods which i can easily apply to any type of Control

You're misunderstanding MVVM. IDataErrorInfo is intended for view model validation, not the control validation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top