用户控件和自定义控件库的区别

时间:2022-11-04 13:21:13

I'm working on creating a date/time user control in WPF using C# 2008. My first user control. I'm also using Matthew MacDonald's book, "Pro WPF in C# 2008". In that book he strongly recommended creating a user control using the WPF Custom Control Library project template; so I followed his suggestion. I've finished writing the code which would go into what I think of as the code-behind file. Now I'm ready to write the XAML.

我正在使用C#2008在WPF中创建日期/时间用户控件。我的第一个用户控件。我也在使用Matthew MacDonald的书“C#2008中的Pro WPF”。在那本书中,他强烈建议使用WPF自定义控件库项目模板创建用户控件;所以我听从了他的建议。我已经完成了编写代码,这些代码将被我认为是代码隐藏文件。现在我准备写XAML了。

The only problem is, I just discovered there is no corresponding .xaml file? So, I don't get why using a WPF Custom Control Library project is better, or prefered, when writing a user control?

唯一的问题是,我刚发现没有相应的.xaml文件?所以,我不明白为什么在编写用户控件时使用WPF自定义控件库项目更好或者更喜欢?

2 个解决方案

#1


29  

A user control and a custom control solve two distinctly different problems.

用户控件和自定义控件解决了两个截然不同的问题。

UserControls are meant to compose multiple WPF controls together, in order to make a set of functionality built out of other controls. This is often used to compose a portion of a window or screen in order to organize your development by allowing you to group multiple pieces of functionality into one "control". For example, if you wanted to make a control for editing a User which provided text boxes for first and last name, age, etc., a single UserControl could be dropped onto a Window and bound to a User instance to edit this. (In this case, you're using standard controls, such as TextBox, to "compose" a control for a more complex purpose.)

UserControls旨在将多个WPF控件组合在一起,以便创建一组由其他控件构建的功能。这通常用于组成窗口或屏幕的一部分,以便通过允许您将多个功能组合成一个“控件”来组织开发。例如,如果您想创建一个控件来编辑提供名字,姓氏,年龄等文本框的用户,可以将一个UserControl拖放到一个Window上并绑定到User实例进行编辑。 (在这种情况下,您使用标准控件(如TextBox)来“组合”控件以实现更复杂的目的。)

A CustomControl, however, is meant to be a new single control. This would typically be a replacement for a built-in control (which could not be redone via templating). I've found that the need for CustomControls is actually fairly rare in WPF, since the WPF templating options and attached properties allow you to do nearly anything with standard controls, once you learn them fully.

但是,CustomControl是一个新的单一控件。这通常是内置控件的替代品(无法通过模板重做)。我发现在WPF中对CustomControls的需求实际上相当少,因为WPF模板选项和附加属性允许你在完全学习它们之后用标准控件做几乎任何事情。

#2


1  

I would also add, if you're intending to inherit from your control, then using a usercontrol is will complicate matters. For example, if create a base usercontrol that has a layout defined in Xaml, WPF framework won't allow you to inherit this control and use Xaml to define the layout for the subclass. In dotnet 3.5 Xaml control can't inherit from another Xaml control

我还想补充一点,如果你打算从你的控件继承,那么使用usercontrol会使问题复杂化。例如,如果创建具有在Xaml中定义的布局的基本用户控件,则WPF框架将不允许您继承此控件并使用Xaml来定义子类的布局。在dotnet 3.5中,Xaml控件无法从另一个Xaml控件继承

#1


29  

A user control and a custom control solve two distinctly different problems.

用户控件和自定义控件解决了两个截然不同的问题。

UserControls are meant to compose multiple WPF controls together, in order to make a set of functionality built out of other controls. This is often used to compose a portion of a window or screen in order to organize your development by allowing you to group multiple pieces of functionality into one "control". For example, if you wanted to make a control for editing a User which provided text boxes for first and last name, age, etc., a single UserControl could be dropped onto a Window and bound to a User instance to edit this. (In this case, you're using standard controls, such as TextBox, to "compose" a control for a more complex purpose.)

UserControls旨在将多个WPF控件组合在一起,以便创建一组由其他控件构建的功能。这通常用于组成窗口或屏幕的一部分,以便通过允许您将多个功能组合成一个“控件”来组织开发。例如,如果您想创建一个控件来编辑提供名字,姓氏,年龄等文本框的用户,可以将一个UserControl拖放到一个Window上并绑定到User实例进行编辑。 (在这种情况下,您使用标准控件(如TextBox)来“组合”控件以实现更复杂的目的。)

A CustomControl, however, is meant to be a new single control. This would typically be a replacement for a built-in control (which could not be redone via templating). I've found that the need for CustomControls is actually fairly rare in WPF, since the WPF templating options and attached properties allow you to do nearly anything with standard controls, once you learn them fully.

但是,CustomControl是一个新的单一控件。这通常是内置控件的替代品(无法通过模板重做)。我发现在WPF中对CustomControls的需求实际上相当少,因为WPF模板选项和附加属性允许你在完全学习它们之后用标准控件做几乎任何事情。

#2


1  

I would also add, if you're intending to inherit from your control, then using a usercontrol is will complicate matters. For example, if create a base usercontrol that has a layout defined in Xaml, WPF framework won't allow you to inherit this control and use Xaml to define the layout for the subclass. In dotnet 3.5 Xaml control can't inherit from another Xaml control

我还想补充一点,如果你打算从你的控件继承,那么使用usercontrol会使问题复杂化。例如,如果创建具有在Xaml中定义的布局的基本用户控件,则WPF框架将不允许您继承此控件并使用Xaml来定义子类的布局。在dotnet 3.5中,Xaml控件无法从另一个Xaml控件继承