自定义控件是否适合MVVM设计

自定义控件是否适合MVVM设计

本文介绍了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的Code-Behind中,并将其用作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的主要区别之外,它是否与View几乎相同(UserControl与CustomControl)?基本上,我的CustomControl真的只是一个View吗?

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

推荐答案

mvvm永远不会完成CustomControl.

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.它只是XAM1,没有任何代码(自动生成的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设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:35