WPF 控件模板与用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1131106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:44:01  来源:igfitidea点击:

WPF ControlTemplate vs UserControl

wpfmvvmuser-controlscontroltemplate

提问by PaN1C_Showt1Me

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

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

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

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

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

现在我意识到我可能只能写一个ControlTemplate。因此,使用 UserControl 有什么好处

回答by Kenan E. K.

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

这里需要考虑三种情况:UserControl、ControlTemplate 和自定义Control。(我猜 DataTemplate 不需要解释)

A custom Controlis 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.

一个自定义的控制是你,当你创建一个新的UI组件的基本功能提供的东西。这有各种优点和缺点,但例如,如果您想要 ItemsControl 的自定义选择行为,您最好通过子类化 Selector 或 MultiSelector(wpftoolkit DataGrid 执行此操作)来实现。此外,如果您想要一个包含新 DependencyProperty 的对象,在大多数情况下您将从 Control 派生。

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.

这里包含的 wpf 原则是“lookless”控件范式,或者“确保有人对您的控件进行模板化,或者至少让它在您自己的模板场景中表现良好”。自定义控件通常在创建时考虑到可重用性,通常作为框架 dll 的一部分。

A ControlTemplateis 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".

的ControlTemplate本质上是一个视觉更换树的描述,并且可以显式地设置FrameworkElements,或作为样式的一部分。当您的目标主要是制作应用程序并完成它时,这是您应该瞄准的选项。如果您能够正确获得绑定和触发器(以及可能包含的 Style 本身),您几乎可以在视觉上使用 ControlTemplate 执行任何操作。所有这些都可以声明为资源并重用,从而为您的应用程序提供一个共同的“主题”。

A UserControlis 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.

一个用户控件是一个自包含的复合控制,与部分在设计师单独可编辑的,而且是最好的使用,如果你需要看你的组件,并在设计管理。另一方面,ControlTemplate 不会公开其组件以在设计器中进行操作(尽管它是可见的)。您通常为客户详细信息页面或产品显示浏览器创建用户控件,或者您不想创建完整控件但想要具有完整设计器支持的详细视图的任何情况。

A special case here is if you use the MVVMpattern. 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模式。许多优秀的 MVVM 实现使用 UserControls 作为视图,使用 ControlTemplates 和 Styles 作为这些视图使用的资源。MVVM 实践还最大限度地减少了对自定义控件的需求,并具有许多其他好处。

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

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

回答by Drew Noakes

If you are adding your own dependency properties, then you will need your own class upon which to define them.

如果您要添加自己的依赖属性,那么您将需要自己的类来定义它们。

As you want to apply a template to the class, this custom class will have to derive from Control(as UserControldoes).

当您想将模板应用于类时,此自定义类必须派生自Control(也是UserControl如此)。

The main benefit of writing your own Control-derived class is that it can have its template redefined for other usage scenarios, either by you within the app, or by other users of the type.

编写自己的 -Control派生类的主要好处是它可以为其他使用场景重新定义其模板,无论是您在应用程序中,还是由该类型的其他用户。

There is very little overhead in using the UserControlclass. In fact, if you look at it in Reflector.NET, you'll see that it hardly has any code. Primarily, UserControljust redefines the metadata on some existing dependency properties (such as making the default value of FocusablePropertyfalse.)

使用UserControl该类的开销很小。事实上,如果您在 Reflector.NET 中查看它,您会发现它几乎没有任何代码。主要UserControl是重新定义一些现有依赖属性的元数据(例如将 . 设为默认值FocusablePropertyfalse

Unless you need to redefine the template of the control immediately, you can leave it as a UserControlfor now and change it later if needed.

除非您需要立即重新定义控件的模板,否则您可以暂时将其保留为 a UserControl,稍后根据需要进行更改。