在 Silverlight 中何时使用 UserControl 与 Control?
我刚刚涉足 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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."
如果您认为您的控件是一组三个文本框,那么
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 aTextBox
then you should extend the existing control with "Make into control."It sounds like you need a
UserControl
to me.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).