WPF ControlTemplate与UserControl [英] WPF ControlTemplate vs UserControl

查看:126
本文介绍了WPF ControlTemplate与UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近制作了一个 UserControl ,这花了很长时间,因为我必须使用自定义的依赖项属性等等.

I've recently made an UserControl, which took quite a long time, because I had to work with custom Dependency Properties and so on...

无论如何,它只是一堆3个控件:TextBox,带有分层树的弹出窗口.

Anyways, it was just a bunch of 3 controls: TextBox, Popup with Hierarchical Tree.

现在我意识到我只能写一个 ControlTemplate .因此,使用UserControl的好处是什么?

Now I realized I could probably write a ControlTemplate only. Hence what is the benefit of using UserControl?

推荐答案

这里需要考虑三种情况:UserControl,ControlTemplate和 custom 控件. (我猜一个DataTemplate不需要解释)

There are three cases to consider here: UserControl, ControlTemplate, and custom Control. (I'm guessing a DataTemplate needs no explanation)

自定义控件是您在创建新的UI组件的基本功能时提供的功能.这样做有很多优点和缺点,但是,例如,如果您想要ItemsControl的自定义选择行为,则最好通过将Selector或MultiSelector子类化(wpftoolkit DataGrid可以做到这一点)来做到这一点.另外,如果您想要一个包含新的DependencyProperty的对象,则在大多数情况下,您将从Control派生.

A custom Control is something you provide when you create base functionality of a new UI component. There are various pros and cons for this, but for example, if you want custom selection behaviour of an ItemsControl, you could best do it by subclassing Selector or MultiSelector (the wpftoolkit DataGrid does this). Also, if you want an object which would contain a new DependencyProperty, you will in most cases derive from Control.

这里包含的wpf原理是看不见的"控件范例,或者一定要期待有人在模仿您的控件,或者至少使其在您自己的模板方案中表现良好".创建自定义控件时通常会考虑到可重用性,通常将它们作为框架dll的一部分.

The wpf principle contained here is the "lookless" control paradigm, or "be sure to expect someone templating your Control, or at least make it behave nicely in your own template scenario". Custom Controls are usually created with reusability in mind, often as parts of framework dlls.

ControlTemplate 本质上是对替换视觉树的描述,可以在FrameworkElements上显式设置,也可以作为样式的一部分进行设置.当您的目标主要是制作应用程序并完成应用程序时,应该使用此选项.如果您能够正确获得绑定和触发器(以及可能包含Style本身),则可以使用ControlTemplate进行可视化操作.可以将所有这些声明为可重用的资源,以使您的应用程序具有共同的主题".

A ControlTemplate is essentially a description of a replacement visual tree, and can be set either explicitly on FrameworkElements, or as a part of a Style. This is the option you should aim at when your goal is primarily to make an application and be done with it. You can do almost anything with a ControlTemplate visually, if you are able to get the bindings and triggers (and the possible containing Style itself) right. All this can be declared as a resource an reused, to give your application a common "theme".

UserControl 是一个独立的复合控件,其中的各个部分可以在设计器中进行单独编辑,如果需要在设计器中查看和管理组件,则最好使用该控件.另一方面,ControlTemplate不会在设计器中公开其组件以供操作(尽管它将可见).通常,您会为客户详细信息"页面或产品"显示浏览器创建UserControl,或者在您不想创建功能全面的控件但希望获得具有完整设计人员支持的详细视图的任何情况下创建该控件.

A UserControl is a self-contained composite control, with parts individually editable in the designer, and is best used if you need to see your components and manage them in the designer. A ControlTemplate, on the other hand, will not expose its components for manipulation in the designer (although it will be visible). You usually create a UserControl for a Customer details page, or a Product display browser, or any case where you don't want to create a full-blown Control, but want detailed view with full designer support.

一种特殊情况是,如果您使用 MVVM 模式.许多出色的MVVM实现使用UserControls作为视图,并使用ControlTemplates和Styles作为这些视图使用的资源. MVVM实践还最大程度地减少了对自定义控件的需求,并具有许多其他好处.

A special case here is if you use the MVVM pattern. Many great MVVM implementations use UserControls as Views, and ControlTemplates and Styles as resources used by those views. MVVM practice also minimizes the need for a custom Control, and has many other benefits.

(有关MVVM的更多信息,包括Google的Josh Smith,Sacha Barber和Karl Shifflett的精彩文章)

(For more information on MVVM, among many others, Google for Josh Smith, Sacha Barber and Karl Shifflett's fantastic articles)

这篇关于WPF ControlTemplate与UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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