WPF 最佳实践:自定义控件是否适用于 MVVM 设计? [英] WPF Best Practices: Do custom controls work well with the MVVM design?

查看:18
本文介绍了WPF 最佳实践:自定义控件是否适用于 MVVM 设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑创建一个可以在我的页面上重复使用的通用控件:一个包含 Address1、Address2、City、State、Zip 等的 AddressControl...

I was looking at creating a common control that I will be able to reuse on my pages: an AddressControl which has Address1, Address2, City, State, Zip, etc...

最初我只是创建了一个包含所有这些项目的类 (AddressEntity) 并实现了 INotifyPropertyChanged.我将该类作为 DependencyProperty 包含在我的 AddressControl 代码隐藏中,并将其用作绑定其属性的 DataContext.

Originally I just created a class (AddressEntity) that contained all these items and implemented INotifyPropertyChanged. I included that class as a DependencyProperty in my Code-Behind for the AddressControl and used it as the DataContext for the bindings to its properties.

然后,有人说我的代码很丑,我应该研究 MVVM.看着它,我假设:

Then, someone said my code was ugly and I should look into MVVM. Looking at it, I assume that:

  • AddressEntity.cs 将只是数据(即 Address1、Address2 等)和成员(即 Clone、ToString 等)的容器
  • 我需要一些 AddressViewModel 来包装我的 AddressEntity 并提供 PropertyNotification 更改、验证等.
  • 我需要为此设置一个视图".

问题是我见过的每个示例都有一个 UserControl 作为 View 而不是 CustomControl.在我深入研究这个之前......

The problem is every example I've ever seen has a UserControl as the View and not a CustomControl. Before I delve too deep into this...

  • 是否可以在此示例中使用 MVVM + 自定义控件?
  • 除了 UserControl 与 CustomControl 的主要区别之外,它是否与视图几乎相同(UserControl 与 CustomControl)?基本上,我的 CustomControl 真的只是一个视图吗?

参考:模型-视图-视图模型 (MVVM) 设计模式用于 WPF

推荐答案

自定义控件永远不会用 mvvm 完成.

CustomControls are never done with mvvm.

您想要的是数据的可重用视图(用户控件),而不是控件(自定义控件).

What you want is a reusable view(user control) of your data and not a control(custom control).

UserControls 和 CustomControls 是两种完全不同的野兽.

UserControls and CustomControls are two completely different beasts.

尽管最初开发 UserControls 的原因是,在 MVVM 中,当您想要特定于模型/视图模型的可重用视图时,通常会使用 UserControl.它只是 XAMl,没有任何代码(自动生成的 InitializeComponent 内容除外).通常,您将 UserControl 保存在使用它的同一个项目中.

Notwithstanding why UserControls were originally developed, in MVVM typically you use a UserControl when you want a reuseable view which is specific to your model/viewmodel. Its just XAMl without any code behind (except for the auto generated InitializeComponent stuff). Generally you keep a UserControl in the same project that you use it in.

当您需要一个需要视图的通用功能时,您会选择 CustomControl,即使在您当前的应用程序范围之外也有潜在用途.这里的控件实际上是在代码文件中定义的,外观(可以被覆盖)通过资源字典中的 XAML 来实现.通常,您将 CustomControl 保留在单独的 ControlLibrary 项目中,并在您希望在其中使用它的项目中引用该库.

You go for a CustomControl when you want a generic piece of functionality which requires a view and which has potential use even outside the scope of your current application. Here the control is actually defined in a code file and the look (which can be overriden) comes via XAML in a resource dictionary. Generally you keep a CustomControl in a a seperate ControlLibrary project and reference the library in the project you wish to use it in.

在充分尊重 WallStreetProgrammer 的情况下,仅根据您是否想要无外观控件在用户控件和自定义控件之间进行选择有点幼稚.

With due respect to WallStreetProgrammer, choosing between a user control and a custom control based solely on whether or not you want a lookless control is a bit naive.

这篇关于WPF 最佳实践:自定义控件是否适用于 MVVM 设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆