我想创建一个通用控件,它可以在我的页面上重复使用: 一个包含Address1、Address2、City、State、Zip等内容的AddressControl。
最初我只是创建了一个类(AddressEntity
),其中包含所有这些项目,并实现了INotifyPropertyChanged
。我在AddressControl的Code-Behind中将该类包含为DependencyProperty,并将其用作绑定其属性的DataContext。
然后,有人说我的代码很丑,建议我研究MVVM。看一下,我认为:
- AddressEntity.cs将只是数据容器(即Address1、Address2等)和成员函数(即Clone、ToString等)
- 我需要一些AddressViewModel来包装我的AddressEntity,并提供属性变更通知、验证等
- 我需要某种方式来展示“View”。
问题在于,我看过的所有示例都是将UserControl作为视图,而不是CustomControl。在深入研究之前...
- 能否在此示例中同时使用MVVM +自定义控件?
- 与UserControl相比,它是否基本相同(CustomControl vs UserControl),除了主要的差异UserControl vs CustomControl外? 基本上,我的CustomControl真的只是一个View吗?