我正在用C#制作一个程序。对于这个程序,我想在我的主表单中添加一些自定义控件。
我创建了自己的进度条,名为' ResourceBar'覆盖ProgressBar。我添加了一些可以在设计器中设置的自定义属性。但是,每当我构建这些值时,都会重置为我不想要的值。
#region Special properties
private Brush fillBrush = new SolidBrush(Color.Black);
[DefaultValue(typeof(Color), "Black")]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("Color of the resource.")]
public Color FillColor
{
get
{
return (fillBrush as SolidBrush).Color;
}
set
{
this.fillBrush = new SolidBrush(value);
}
}
private Brush fontBrush = new SolidBrush(Color.Black);
[DefaultValue(typeof(Color), "Black")]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("Font Color of the text above the resource bar.")]
public Color FontColor
{
get
{
return (fontBrush as SolidBrush).Color;
}
set
{
this.fontBrush = new SolidBrush(value);
}
}
private bool drawIfNonZero;
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
public bool DrawIfNonZero
{
get { return drawIfNonZero; }
set { drawIfNonZero = value; }
}
private bool drawMaximum;
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
public bool DrawMaximum
{
get { return drawMaximum; }
set { drawMaximum = value; }
}
private bool fillResourceBar;
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
public bool FillResourceBar
{
get { return fillResourceBar; }
set { fillResourceBar = value; }
}
#endregion
之前我没有支持领域。我添加了他们希望它能解决我的问题,但它没有解决它。
答案 0 :(得分:0)
@Hans Passant,
谢谢!这确实是错误。 我通过添加' = true'
来修复它 [DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
public bool DrawIfNonZero
{
get; set;
}
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
public bool DrawMaximum
{
get; set;
} = true;
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
public bool FillResourceBar
{
get; set;
} = true;
也删除了备用字段。