子类控件 [英] Subclassing controls

查看:48
本文介绍了子类控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出如何子类化控件并覆盖一些默认属性。我可以子类化一个控件并添加我自己的新属性并设置它们的默认值。但是如何更改*现有*属性的默认值?


对于自定义属性,我可以这样做:


[DefaultValue (FormatType.Default)]

public FormatType FormatAs

{

get {return this.formatAs; }

set {this.formatAs = value; }

}


但是当我尝试使用现有属性的代码结构时,它不会编译。我错过了什么?


TIA,


Mike Rodriguez

I''m trying to figure out how to subclass a control and override some of the default properties. I can subclass a control and add my own new properties and set their default values. But how do you change the default value for an *existing* property?

For a custom property I can do this:

[DefaultValue(FormatType.Default)]
public FormatType FormatAs
{
get { return this.formatAs; }
set { this.formatAs = value; }
}

But when I try that code structure with an existing property it won''t compile. What am I missing?

TIA,

Mike Rodriguez

推荐答案

你必须覆盖或新或新继承的属性然后你可以

提供你自己的实现,或称为base。允许基地提供实现,然后提供适当的属性,因为你已经完成了。


- -

Tim Wilson

..Net Compact Framework MVP


" Michael Rodriguez" < MI ** @ nospamforme.com>在留言中写道

news:eQ ************** @ TK2MSFTNGP14.phx.gbl ...

我正在尝试找出如何子类化控件并覆盖一些

默认属性。我可以子类化一个控件并添加我自己的新属性

并设置它们的默认值。但是如何更改

和*现有*属性的默认值?


对于自定义属性,我可以这样做:


[DefaultValue(FormatType.Default)]

public FormatType FormatAs

{

get {return this.formatAs; }

set {this.formatAs = value; }

}


但是当我尝试使用现有属性的代码结构时,它不会是
编译。我错过了什么?


TIA,


Mike Rodriguez
You''ll have to "override" or "new" the inherited property and then you can
provide your own implementation, or call the "base" to allow the base to
provide the implementation, and then provide the proper attributes, as you
have done.

--
Tim Wilson
..Net Compact Framework MVP

"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I''m trying to figure out how to subclass a control and override some of the
default properties. I can subclass a control and add my own new properties
and set their default values. But how do you change the default value for
an *existing* property?

For a custom property I can do this:

[DefaultValue(FormatType.Default)]
public FormatType FormatAs
{
get { return this.formatAs; }
set { this.formatAs = value; }
}

But when I try that code structure with an existing property it won''t
compile. What am I missing?

TIA,

Mike Rodriguez


有几种更改现有

属性默认值的方法。

最快最肮脏的方法是在构造函数中或在

OnInit中字段设置具有特定值的属性

覆盖OnInit(...)

{

FormatType = MyFormatType.Bleah;

.....

base.OnInit(...);

}

There are several methods to changing the default value for an existing
property.
The quickest and dirtiest way is either in your constructor or in the
OnInit field to set the property with a specific value
override OnInit(...)
{
FormatType = MyFormatType.Bleah;
.....
base.OnInit(...);
}


我会从Windows窗体的角度讲,因为我不是网络开发人员。

这个方法的问题是,虽然你将改变最初的

属性的值,它会将此值序列化为

InitializeComponent方法,并在属性窗口中以粗体显示。如果

那么这不是问题,那么,这不是问题。但是对于一个不错的

设计时体验,使用DefaultValueAttribute作为简单的默认值,

以及复杂默认值的ShouldSerializeX和ResetX方法,就是这样的方式

去吧。


-

Tim Wilson

..Net Compact Framework MVP


< ma ********** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
I''ll speak from a Windows Forms perspective, since I''m not a web developer.
The problem with this method is that, while you will change the initial
value of the property, it will serialize this value into the
InitializeComponent method and show it in bold in the properties window. If
this is not a problem then, well, it''s not a problem. But for a nice
design-time experience, using the DefaultValueAttribute for simple defaults,
and the ShouldSerializeX and ResetX methods for complex defaults, is the way
to go.

--
Tim Wilson
..Net Compact Framework MVP

<ma**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
有几种方法可以更改现有
属性的默认值。
最快和最脏的方法是在构造函数中或在
OnInit字段中设置属性特定值
覆盖OnInit(...)
{
FormatType = MyFormatType.Bleah;
....
base.OnInit(...);
}
There are several methods to changing the default value for an existing
property.
The quickest and dirtiest way is either in your constructor or in the
OnInit field to set the property with a specific value
override OnInit(...)
{
FormatType = MyFormatType.Bleah;
....
base.OnInit(...);
}



这篇关于子类控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆