【发布时间】:2016-09-27 22:37:08
【问题描述】:
尝试在运行时设置默认值,我的 propertygrid 使用了以下类:
public class zPosition
{
public int _x;
public int _y;
public zPosition(int x, int y, int dx = 0, int dy = 0)
{
this._x = 10;
this._y = 10;
// set the default values here
}
[DisplayName("X"), DefaultValueAttribute(0)]
public int X
{
get { return _x; }
set { _x = value; }
}
[DisplayName("Y"), DefaultValueAttribute(0)]
public int Y
{
get { return _y; }
set { _y = value; }
}
}
如何在类构造函数中设置这些默认值?
谢谢
【问题讨论】:
-
DefaultValue 仅供设计者使用。它不设置任何值。当属性值与 DefaultValue 属性值匹配时,设计器不会将信息保存在 Designer 文件中。
-
是否有解决方法来设置 propertygrid 属性的默认值?我需要在运行时设置它们而不是使用以下内容: DefaultValueAttribute(0)
-
你会如何在运行时使用它?
-
你不能用属性来做,写私有
bool ShouldSerializeXxxx()和void ResetXxxx()方法代替Xxxx匹配属性名称。
标签: c# .net propertygrid