在 Silverlight 中何时使用 UserControl 与 Control?

发布于 2024-08-16 03:14:10 字数 295 浏览 4 评论 0原文

我刚刚涉足 Silverlight,并没有真正理解创建 UserControl 与为同一任务创建 Control 的区别和优缺点(如右键单击时)例如,在 Expression Blend 中进行选择)。

看起来选择“Make Into Control”只是为您指定的基类型创建一个新模板,而创建 UserControl 则创建一个全新的基类。这是正确的吗?

在此特定实例中,我正在创建一个仅接受数字的自定义文本框控件,并将其自身分为 3 个部分,将 3 个值存储到单独的属性中,如下图所示。在这种特殊情况下,哪个最好?

I'm just getting my feet wet in Silverlight, and don't really understand the differences and pros/cons of creating a UserControl vs. creating a Control for the same task (as in when you right click on a selection in Expression Blend, for instance).

It seems like selecting "Make Into Control" just creates a new template for the base type you specify, whereas creating a UserControl creates a whole new base class. Is that correct?

In this particular instance, I'm creating a custom text box control that only takes numbers, and divides itself into 3 sections, storing 3 values into separate properties as pictured below. In this particular case, which would be best?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶色山野 2024-08-23 03:14:10

UserControls 是一个复合控件——基本上是一堆其他“控件”组合在一起作为一个单一的、有凝聚力的单元。

另一方面,自定义控件旨在用作单个控件。想想框架中的基本控件,例如 TextBox 或 Button - 如果您要实现类似的东西,您会需要一个 Control。 (这比 UserControls 不太常见,尤其是在 WPF 中,因为您可以在基类控件上使用模板来完成许多需要在其他框架中自定义控件的事情)。自定义控件就是为单个“控件”定义新行为。

UserControls are meant to be a composite control - basically a bunch of other "controls" grouped together to work as a single, cohesive unit.

Custom Controls, on the other hand, are intended to be used as a single control. Think of the basic controls in the framework, such as TextBox or Button - if you were implementing something like that, you'd want a Control. (This is less common than UserControls, especially in WPF, since you can use templating on base class controls to accomplish quite a few things where you'd need custom controls in other frameworks). A custom Control is all about defining new behavior for a single "control."

我喜欢麦丽素 2024-08-23 03:14:10

如果您认为您的控件是一组三个文本框,那么 UserControl 将是合适的,但如果您的控件本质上仍然TextBox > 那么您应该使用“Make into control”来扩展现有的控件。

对我来说,您似乎需要一个 UserControl

If you consider your control to be a group of three text boxes then a UserControl would be appropriate, but if your control will still essentially be a TextBox then you should extend the existing control with "Make into control."

It sounds like you need a UserControl to me.

猫烠⑼条掵仅有一顆心 2024-08-23 03:14:10

Dov,我认为您已经通过更新回答了您自己的问题。当您想要制作支持模板化的控件时,自定义控件最有用。否则,当您从其他控件继承以干净地添加功能(TextBox -> PasswordTextBox)时,它们非常有用。

Dov, I think you've answered your own question with your update. Custom Controls are most useful when you want to make a control that supports templating. Otherwise they are useful when you are inheriting from other controls to cleanly add functionality (TextBox -> PasswordTextBox).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文