WPF ControlTemplate 与 UserControl [英] WPF ControlTemplate vs UserControl

查看:31
本文介绍了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 和自定义 Control.(我猜 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 不会公开其组件以便在设计器中进行操作(尽管它是可见的).您通常为客户详细信息页面或产品显示浏览器创建用户控件,或者在您不想创建完整控件但想要具有完整设计器支持的详细视图的任何情况下.

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 for 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天全站免登陆