如何设置Winforms自定义控件中Colors的默认值?
我使用以下方法获得了正确显示的值:
[DefaultValue ( typeof ( Color ), "255, 0, 0" )]
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
但是在重新加载项目后,使用了控件,该值设置为白色,我可以调用重置以再次返回到红色,但我不明白这个问题。
除非我手动更改默认值,否则您应该如何设置默认值并确保保留它?
实际上我也在这样做,它将 Back 和 ForeColor 设置为这些值,并且 VS 属性编辑器显示它们,就像它们从默认值更改一样。
这是错误的吗?
public CoolGroupBox ( )
{
InitializeComponent ( );
base.BackColor = Color.FromArgb ( 5, 5, 5 );
base.ForeColor = Color.FromArgb ( 0, 0, 0 );
}
I got the value to show up correctly using:
[DefaultValue ( typeof ( Color ), "255, 0, 0" )]
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
But after I reload the project the control is used, this value is set to White, which I can invoke Reset to get back to Red again, but I don't understand the problem.
How are you supposed to set the default value and make sure it's preserved unless I change the value manually from the default?
Actually I am also doing this, which sets Back and ForeColor to these values and the VS property editor shows them as if they are changed from the default value.
Is this wrong?
public CoolGroupBox ( )
{
InitializeComponent ( );
base.BackColor = Color.FromArgb ( 5, 5, 5 );
base.ForeColor = Color.FromArgb ( 0, 0, 0 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
诀窍是使用颜色的十六进制代码:
我认为您也可以使用“255,0,0”,但我不确定并且通常使用指定颜色或十六进制代码。
The trick is to use the Hex code of the color:
I think you can also use "255, 0, 0" but am not sure and have normally used either the named colors or the hex code.
将私有成员变量设置为您想要的默认颜色怎么样?
如果您希望保留它,只需取出 set 访问器即可。
编辑
我明白了,您希望设计器中的属性列表显示默认颜色。
您必须重写基本控件的 BackColor 属性,为您的 new 属性添加新的 DefaultValueAttribute,然后在构造函数或 InitializeComponent() 方法(在设计器中)中实际设置默认颜色。 cs 文件),这可能更好,因为这是为设计者准备的。
What about just setting the private member variable to the default color you want?
If you want it preserved, just take out the set accessor.
Edit
I see, you want the property list in the designer to show the default color.
You have to override the BackColor property of the base control, add a new DefaultValueAttribute for your new property, and then actually set the default color in the constructor or in the InitializeComponent() method (in the designer.cs file), which is probably better since this is for the designer.
[DefaultValue(...)]
属性是对设计者和代码生成器的提示。它不是给编译器的指令。有关详细信息,请参阅此知识库文章。
The
[DefaultValue(...)]
attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.More info in this KB article.
我没有运气使用具有
Color
类型或Font
类型的属性的DefaultValue
属性,但我确实成功地使用了所描述的这些方法在 msdn 文档中:“使用 ShouldSerialize 和 Reset 方法定义默认值”
http://msdn.microsoft.com/en- us/library/53b8022e(v=vs.90).aspx
我分别使用
Color.Empty
和null
作为我的私有支持字段的值并让公共财产总是返回一些有用的东西。I didn't have any luck using the
DefaultValue
attribute with properties of typeColor
or of typeFont
, but I did succeed with these methods described in the msdn documentation:"Defining Default Values with the ShouldSerialize and Reset Methods"
http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx
I used
Color.Empty
andnull
, respectively, as the values for my private backing fields and had the public properties always return something useful.您可以在 InitializeComponent 方法中的 component.designer.cs 中设置特定属性:
重新构建项目,所有内容也应该显示在
You can set specific Attribute in the component.designer.cs in the Initializecomponent Method:
Rebuild the project and everything should also show up in the
我使用了这段代码,它运行得很好
I used this code and it worked perfectly
CodeProject
There is quite an article about defaultvalue property initialization on CodeProject
在 DependencyProperty 中使用 UIPropertyMetadata,
例如:
In DependencyProperty use a UIPropertyMetadata
like: