简体   繁体   English

C#-Silverlight-自定义控件还是UserControl?

[英]C# - Silverlight - Custom control or UserControl?

I need a button that is visually completely customizable, but has custom logic to publish events and manage it's visual state based on events it has registered for. 我需要一个按钮,该按钮在视觉上可以完全自定义,但是具有自定义逻辑来发布事件并基于已注册的事件来管理其视觉状态。

When I say visually customizable, I mean I should be able to both create the button in xaml and set it's style by binding to the supplied style. 当我说视觉上可定制时,我的意思是我应该既可以在xaml中创建按钮,又可以通过绑定到提供的样式来设置其样式。 Or I can create an instance of the button and set the style by passing a parameter to an alternate constructor. 或者,我可以创建按钮的实例并通过将参数传递给备用构造函数来设置样式。 Or by calling a method on the button class to set the style. 或通过在按钮类上调用方法来设置样式。

I do not plan on substituting the controls template, it should be a button. 我不打算替换控件模板,它应该是一个按钮。 Can anyone point me to some code samples of this? 谁能指出我的一些代码示例?

If you don't want to allow the control to be templated (I would generally recommend you do, but you don't have to) than a UserControl would work well. 如果您不想让控件模板化(我通常会建议您这样做,但您不必这样做),那么UserControl会很好地工作。 A simple user control that exposes a ButtonStyle property that is applied to a specific control should solve your needs. 一个简单的用户控件公开一个应用于特定控件的ButtonStyle属性,可以解决您的需求。

Totally depends on your application. 完全取决于您的应用程序。 I create ContentControl derivatives much more often than UserControl, if only for the glint of hope that they may be useful in the future. 如果只是为了希望它们将来可能有用,我会比UserControl更频繁地创建ContentControl派生类。

I have written before: 我以前写过:

UserControls are content controls that are optimized for simple design-time use, typically for sharing within an application, but not with other applications. UserControl是为简单设计时使用而优化的内容控件,通常用于在一个应用程序中共享,但不能与其他应用程序共享。

Component libraries should never contain UserControl implementations. 组件库永远不应包含UserControl实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM