自定义与用户控制 [英] Custom vs User control

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

问题描述

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

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

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

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.

在此示例中,我应该创建用户控件还是自定义控件?既然我将拥有属性和逻辑,那么我应该为此控件提供一个视图模型吗?

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?

编辑:您知道这两个选项之间的概念清晰分开的文章吗?


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

推荐答案

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

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

我遵循以下考虑顺序


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

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

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

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

自定义控件:如果控件模板无法执行此操作,则使用自定义控件。如果我需要自定义/扩展已经存在的控件。提供基于GridView中的标题行进行排序,筛选的示例(Metro应用中存在GridView,仅用于说明示例)

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)

用户控件:最不喜欢的一个。仅当需要合成时,并且我无法使用自定义控件进行合成。像您的示例一样,有2个Combobox和1个datagrid。用户控件没有提供无缝的无缝外观功能,可以通过自定义控件或控件模板加以利用。

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.

这篇关于自定义与用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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