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.
但是在我重新加载项目之后使用了控件,这个值被设置为White,我可以调用Reset来再次回到Red,但是我不明白这个问题。
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.
实际上我也在这样做,它将Back和ForeColor设置为这些值,VS属性编辑器将它们显示为从默认值更改它们。
Is this wrong?
这是错的吗?
public CoolGroupBox ( )
{
InitializeComponent ( );
base.BackColor = Color.FromArgb ( 5, 5, 5 );
base.ForeColor = Color.FromArgb ( 0, 0, 0 );
}
7 个解决方案
#1
4
The [DefaultValue(...)]
attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.
[DefaultValue(...)]属性是设计者和代码生成器的提示。它不是编译器的指令。
More info in this KB article.
此知识库文章中的更多信息。
#2
17
The trick is to use the Hex code of the color:
诀窍是使用颜色的Hex代码:
[DefaultValue(typeof(Color), "0xFF0000")]
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
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.
我认为你也可以使用“255,0,0”,但我不确定,并且通常使用命名颜色或十六进制代码。
#3
8
What about just setting the private member variable to the default color you want?
将私有成员变量设置为您想要的默认颜色怎么样?
private Color lineColor = Color.Red;
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
If you want it preserved, just take out the set accessor.
如果你想保留它,只需取出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.
您必须覆盖基本控件的BackColor属性,为新属性添加新的DefaultValueAttribute,然后在构造函数或InitializeComponent()方法(在designer.cs文件中)实际设置默认颜色,这可能是更好,因为这是为设计师。
public partial class RedBackgroundControl : UserControl
{
public RedBackgroundControl()
{
InitializeComponent();
base.BackColor = Color.Red;
}
[DefaultValue(typeof(Color), "Red")]
new public Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
}
#4
0
I didn't have any luck using the DefaultValue
attribute with properties of type Color
or of type Font
, but I did succeed with these methods described in the msdn documentation:
我没有运气使用具有Color类型或Font类型属性的DefaultValue属性,但我确实成功使用了msdn文档中描述的这些方法:
"Defining Default Values with the ShouldSerialize and Reset Methods" http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx
“使用ShouldSerialize和Reset方法定义默认值”http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx
I used Color.Empty
and null
, respectively, as the values for my private backing fields and had the public properties always return something useful.
我分别使用Color.Empty和null作为我的私有支持字段的值,并且公共属性总是返回一些有用的东西。
#5
0
You can set specific Attribute in the component.designer.cs in the Initializecomponent Method:
您可以在Initializecomponent方法的component.designer.cs中设置特定属性:
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.LineColor= System.Drawing.Color.FromArgb(255, 0, 0);
}
Rebuild the project and everything should also show up in the
重建项目,一切也应该出现在
#6
-1
There is quite an article about defaultvalue property initialization on CodeProject
有一篇关于CodeProject上的defaultvalue属性初始化的文章
#7
-1
In DependencyProperty use a UIPropertyMetadata
在DependencyProperty中使用UIPropertyMetadata
like:
public static DependencyProperty TrueColorProperty = DependencyProperty.Register(
"TrueColor", typeof (Color), typeof (LedControl), new UIPropertyMetadata(Colors.Red));
#1
4
The [DefaultValue(...)]
attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.
[DefaultValue(...)]属性是设计者和代码生成器的提示。它不是编译器的指令。
More info in this KB article.
此知识库文章中的更多信息。
#2
17
The trick is to use the Hex code of the color:
诀窍是使用颜色的Hex代码:
[DefaultValue(typeof(Color), "0xFF0000")]
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
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.
我认为你也可以使用“255,0,0”,但我不确定,并且通常使用命名颜色或十六进制代码。
#3
8
What about just setting the private member variable to the default color you want?
将私有成员变量设置为您想要的默认颜色怎么样?
private Color lineColor = Color.Red;
public Color LineColor
{
get { return lineColor; }
set { lineColor = value; Invalidate ( ); }
}
If you want it preserved, just take out the set accessor.
如果你想保留它,只需取出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.
您必须覆盖基本控件的BackColor属性,为新属性添加新的DefaultValueAttribute,然后在构造函数或InitializeComponent()方法(在designer.cs文件中)实际设置默认颜色,这可能是更好,因为这是为设计师。
public partial class RedBackgroundControl : UserControl
{
public RedBackgroundControl()
{
InitializeComponent();
base.BackColor = Color.Red;
}
[DefaultValue(typeof(Color), "Red")]
new public Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
}
#4
0
I didn't have any luck using the DefaultValue
attribute with properties of type Color
or of type Font
, but I did succeed with these methods described in the msdn documentation:
我没有运气使用具有Color类型或Font类型属性的DefaultValue属性,但我确实成功使用了msdn文档中描述的这些方法:
"Defining Default Values with the ShouldSerialize and Reset Methods" http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx
“使用ShouldSerialize和Reset方法定义默认值”http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx
I used Color.Empty
and null
, respectively, as the values for my private backing fields and had the public properties always return something useful.
我分别使用Color.Empty和null作为我的私有支持字段的值,并且公共属性总是返回一些有用的东西。
#5
0
You can set specific Attribute in the component.designer.cs in the Initializecomponent Method:
您可以在Initializecomponent方法的component.designer.cs中设置特定属性:
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.LineColor= System.Drawing.Color.FromArgb(255, 0, 0);
}
Rebuild the project and everything should also show up in the
重建项目,一切也应该出现在
#6
-1
There is quite an article about defaultvalue property initialization on CodeProject
有一篇关于CodeProject上的defaultvalue属性初始化的文章
#7
-1
In DependencyProperty use a UIPropertyMetadata
在DependencyProperty中使用UIPropertyMetadata
like:
public static DependencyProperty TrueColorProperty = DependencyProperty.Register(
"TrueColor", typeof (Color), typeof (LedControl), new UIPropertyMetadata(Colors.Red));