wpf 自定义与用户控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11247708/
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 23:27:49  来源:igfitidea点击:

Custom vs User control

wpfuser-controlswpf-controlscustom-controlsviewmodel

提问by Louro

I've been reading some explanations about the difference between User and Custom Controls, for example this: http://www.wpftutorial.net/CustomVsUserControl.html

我一直在阅读有关用户控件和自定义控件之间区别的一些解释,例如:http: //www.wpftutorial.net/CustomVsUserControl.html

I want to create, for example, a simple composition of a datagrid with 2 comboboxes which are responsible to change the values from the datagrid's items. I want to create a specific control for this because I'm going to use it a lot of times. I would like to implement the logic behind and then in the xaml invocation I only have to specify the itemsSource.

例如,我想创建一个简单的数据网格组合,其中包含 2 个组合框,这些组合框负责更改数据网格项目中的值。我想为此创建一个特定的控件,因为我将多次使用它。我想实现背后的逻辑,然后在 xaml 调用中我只需要指定 itemsSource。

For this example should I create a User or Custom control? Since I will have properties and logic, should I have a viewmodel for this control?

对于这个例子,我应该创建一个用户控件还是自定义控件?既然我将拥有属性和逻辑,我应该为这个控件设置一个视图模型吗?

EDIT: Do you know some articles with clear conceptual separation between these 2 options?

编辑:你知道一些文章在这两个选项之间有明确的概念分离吗?

回答by Tilak

Choice is not only between user control and custom control, but among user control, custom control, customizing control template, customizing data template, header template (for collection based controls), attached properties. Refer to Control Authoring overview

选择不仅在用户控件和自定义控件之间,还包括用户控件、自定义控件、自定义控件模板、自定义数据模板、标题模板(用于基于集合的控件)、附加属性。请参阅控件创作概述

I go by following order of consideration

我按照考虑的顺序

  1. Attached Properties: If functionality can be achieved, I use attached properties. Example, Numeric text box.

  2. Control Template: When requirement can be fulfilled by customizing the control template, I use this. Example, circular progress bar.

  3. Custom control: If control template cannot do it, I use custom control. Provided I need to customize/extend already present control. Example providing Sorting, Filtering based on header row in GridView (GridView is present in metro apps, used just to illustrate the example)

  4. User control: Least preferred one. Only when composition is required, and I am unable to do it using custom control. Like in your example, 2 Combobox, and 1 datagrid. User controls does not provide seamless lookless feature that can be leveraged through custom control or control template.

  1. 附加属性:如果可以实现功能,我会使用附加属性。例如,数字文本框。

  2. 控制模板:当可以通过自定义控制模板来满足要求时,我使用它。例如,圆形进度条。

  3. 自定义控件:如果控件模板不能做到,我使用自定义控件。前提是我需要自定义/扩展已经存在的控件。提供基于 GridView 中的标题行进行排序、过滤的示例(GridView 存在于 Metro 应用程序中,仅用于说明示例)

  4. 用户控制:最不喜欢的一种。仅当需要组合时,我无法使用自定义控件来完成。就像在您的示例中一样,2 个组合框和 1 个数据网格。用户控件不提供可通过自定义控件或控件模板利用的无缝外观功能。

回答by Benjamin Gale

You already have some great answers that explain the differences but also understand that custom controls and UserControlshave different purposes:

您已经有了一些很好的答案来解释差异,但也了解自定义控件UserControls具有不同的用途:

A UserControltypically encapusulates some sort of composite behaviour. If you have an application that needs to edit contact details in many places, for example, you could create a custom control that has the labels and text fields for all the data laid out with a submit button that has the relevant code and reuse this control throughout your application.

AUserControl通常封装某种复合行为。例如,如果您的应用程序需要在许多地方编辑联系人详细信息,您可以创建一个自定义控件,其中包含所有数据的标签和文本字段,并使用具有相关代码的提交按钮并重用此控件在整个应用程序中。

A custom control is a control that is derived from one of the WPFcontrol classes (E.G. Control, ContentControletc.) and has to be created in code. These control usually have a single cohesive purpose (think TextBox, ComboBox, Label) rather than acting together as a whole (although this doesn't have to be the case).

自定义控件是从WPF控件类(EG等)之一派生的控件ControlContentControl必须在代码中创建。这些控件通常具有单一的内聚目的(认为TextBox, ComboBox, Label)而不是作为一个整体一起行动(尽管情况并非必须如此)。

UserControl's are usually easier for people unfamiliar with WPFas they can be visually designed.

UserControl's 通常对于不熟悉的人来说更容易,WPF因为它们可以进行视觉设计。

My suggestion would be to start off with a UserControl. You can always refactor this into a custom control at a later date as you become more familiar with the way WPFworks. Creating your control as a custom control willrequire knowledge of ControlTemplates and Styles as you will need to provide your own to define a look and feel for your control.

我的建议是从UserControl. 随着您对工作方式越来越熟悉,您可以随时将其重构为自定义控件WPF。创建控件作为自定义控制需要的知识ControlTemplateS和StyleS作为你将需要提供自己的定义您的控制的外观和感觉。

When all is said and done, as long as the control behaves correctly, it doesn't matter which approach you use.

当一切都完成后,只要控件行为正确,使用哪种方法并不重要。

See thispost for an example of two approaches to the same problem. The post author wanted a control which can present modal content in front of the primary content. The post author actually answered his own question by implementing it as a UserControl. I have added an answer to the post which creates the control as a custom control but both have the same end effect.

有关解决同一问题的两种方法的示例,请参阅帖子。帖子作者想要一个控件,可以在主要内容之前呈现模态内容。帖子作者实际上通过将其实现为UserControl. 我在帖子中添加了一个答案,该答案将控件创建为自定义控件,但两者具有相同的最终效果。

回答by dowhilefor

The best explanation is in the msdn. CustomControl is more a "virtual" name, there is no class called "CustomControl" in WPF, instead its meant creating a new class building on top of one of WPF control classes, like Control, ItemsControland even more specific Controls like TextBoxor Button.

最好的解释是在msdn 中。CustomControl 更像是一个“虚拟”名称,WPF 中没有名为“CustomControl”的类,而是意味着在 WPF 控件类之一之上创建一个新类,例如ControlItemsControl甚至更具体的控件,例如TextBoxButton

For your specific case, a UserControl should be enough, creating a CustomControl is something that can easily be avoided. While its not a bad thing, a lot of people, especially beginners in WPF coming from WinForms tend to subclass more then necessary.

对于您的特定情况,一个 UserControl 应该就足够了,创建一个 CustomControl 是很容易避免的。虽然这不是一件坏事,但很多人,尤其是来自 WinForms 的 WPF 初学者倾向于子类化,而不是必要的。

回答by G.Y

  • If you have a view-modeland you wish to create a view for it use the User-Control.

  • If you need an autonomouscontrol that has no specific view-model,
    you probably need a custom-control.

  • If you find that the functionality you need as whole, already existin other controls you need to override an existing control template.
    (i.e: for a diamond shaped button - you need to override the button control template.)

  • Regarding attached-properties and attached-behaviors, those are useful when you have a control which you want to extend with more properties or you want it to behave slightly different than its default behavior.

  • 如果您有一个视图模型并且您希望为其创建一个视图,请使用User-Control

  • 如果您需要一个没有特定视图模型的自主控件,
    您可能需要一个custom-control

  • 如果您发现您需要的整体功能已经存在于其他控件中,您需要覆盖现有的控件模板
    (即:对于菱形按钮 - 您需要覆盖按钮控制模板。)

  • 关于附加属性和附加行为,当您有一个控件想要扩展更多属性或者您希望它的行为与其默认行为略有不同时,这些非常有用。

In the provided case of the composition the OP described, it can be achieved with either user control or custom control. I would prefer a custom control since there is no specific view model provided, the "input" is only a property bound to an item collection.

在 OP 描述的组合提供的情况下,它可以通过用户控件或自定义控件来实现。我更喜欢自定义控件,因为没有提供特定的视图模型,“输入”只是绑定到项目集合的属性。

Oh, and, I am sorry for slightly being late.

哦,还有,我有点迟到了。

回答by Riva

You can easily Visually design CustomControl. Create new UserControl(or Window). Create its xaml structure visually in Designer. Copy-paste body of the resulting xaml inside ControlTemplateof your new CustomControl(Eg. in generic theme file).

您可以轻松地进行视觉设计CustomControl。创建新的UserControl(或窗口)。在 Designer 中直观地创建其 xaml 结构。将生成的 xaml 的正文复制粘贴到ControlTemplate新的内部CustomControl(例如,在通用主题文件中)。

If I remember right, you are also able to visually design CustomControl template directly, in Blend.

如果我没记错的话,您还可以直接在 Blend 中直观地设计 CustomControl 模板。

Of course you can also instance the wip CustomControl in a Window and put the Window's Designer view as new panel above the control's xaml view in VisualStudio. Some xaml bindings from style template don't show in Designer like this though, until I rebuild.

当然,您也可以在窗口中实例化 wip CustomControl,并将窗口的设计器视图作为新面板放在 VisualStudio 中控件的 xaml 视图上方。样式模板中的一些 xaml 绑定不会像这样显示在 Designer 中,直到我重建。

[ Imho GUI is mainly a visual matter and should not, and doesn't need to, be created in code. ]

[ Imho GUI 主要是一个视觉问题,不应该也不需要在代码中创建。]

回答by NestorArturo

If this is somehow your first time building controls, I recommend UserControl as VS lets you design its interface more easily. Custom Controls are more powerful, but you have to CLEARLY separate your control's logic from its interface and this requires a bit more preparation.

如果这是您第一次构建控件,我推荐 UserControl,因为 VS 可以让您更轻松地设计其界面。自定义控件功能更强大,但您必须将控件的逻辑与其界面清楚地分开,这需要更多的准备。

回答by MBen

Well to create a Custom control you need to implement it as a User control. Your own User control is called a Custom control. It is pretty simple.

要创建自定义控件,您需要将其实现为用户控件。您自己的用户控件称为自定义控件。这很简单。

UserControl is the base class for containing your custom content :

UserControl 是包含自定义内容的基类:

<UserControl>
  Your custom WPF content
</UserControl>

I don't totally agree with the article. However in your case you need a UserControl that you can re-use latter in your UI.

我不完全同意这篇文章。但是,在您的情况下,您需要一个可以在 UI 中重用的 UserControl。