如何向我的控件添加新的现有属性?
我有自己的控件:
public class newControl : Control
{
}
有一个 Text
属性,但没有 TextAlign
属性。例如,我需要这个类似于 Button
的 TextAlign
属性的属性,但我不想从按钮类继承它。
那么我可以只继承 TextAlign
属性吗?如果是,怎么办?
I have my own control:
public class newControl : Control
{
}
There is a Text
property, but there is not a TextAlign
property. For example, I need this property similar to the TextAlign
property of a Button
, but I don't want inherit it from a button class.
So can I inherit only TextAlign
property? If yes, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您只需添加即可。内置枚举称为
ContentAlignment
:您现在可以决定如何使用此属性。
请注意,我添加了一些有关如何在
PropertyGrid
中使用控件的属性。DefaultValue
属性不会设置属性的值,它只是确定属性是否以粗体显示。要使用
TextAlign
属性显示文本,您必须重写OnPaint
方法并绘制它:Yes, you can just add it. The built in enumeration is called
ContentAlignment
:What you do with this property is up to you now.
Note that I added some attributes for how the control is used in the
PropertyGrid
. TheDefaultValue
attribute does not set the value of the property, it just determines whether or not the property is displayed in bold or not.To display the text using your
TextAlign
property, you would have to override theOnPaint
method and draw it:首先,考虑继承 System.Web.UI。 WebControls.WebControl,它有更多与样式对应的属性,例如
CssClass
和Attributes
。我建议不要使用
TextAlign
属性,而是简单地将 CSS 类添加到您的页面,并使用 setCssClass
属性,该属性位于基类WebControl< /代码>。
或者,您可以通过执行以下操作来设置文本对齐方式(但 CSS 类会更清晰):
当然,您也可以随时添加自己的属性,将正确的 CSS 写入
Attributes
集合。First, consider inheriting from System.Web.UI.WebControls.WebControl, which has more properties that correspond to styling, such as
CssClass
andAttributes
.Rather than using the
TextAlign
property, I would recommend simply adding a CSS class to your page, and use setCssClass
property, which is on the base classWebControl
.Alternatively, you could set the text alignment by doing the following (but a CSS class would be cleaner):
Of course, you could always add your own property that writes the correct CSS to the
Attributes
collection as well.