【发布时间】:2023-04-08 09:29:02
【问题描述】:
我正在创建一个自定义控件,其中我正在创建“列表”类型的属性
Sections 是一个公共类,有 4 个属性。
控件中的代码如下所示:
public partial class genericGauge : Control
{
public genericGauge()
{
InitializeComponent();
}
// Stripped out code not needed for this issue question.
private List<Sections> indicators = new List<Sections>();
public List<Sections> Indicators
{
get
{
return indicators;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Stripped out code not needed for this issue question.
}
}
Sections 类如下:
public class Sections
{
private string header = "Section1";
public string Header
{
get {return header;}
set
{
header = value;
}
}
private float startvalue = 0.0f;
public float StartValue
{
get { return startvalue; }
set
{
startvalue = value;
}
}
private float sweepvalue = 0.0f;
public float SweepValue
{
get { return sweepvalue; }
set
{
sweepvalue = value;
}
}
private Color sectioncolor = new Color();
public Color SectionColor
{
get {return sectioncolor;}
set
{
sectioncolor = value;
}
}
}
除了当我在设计时使用属性浏览器类型编辑器将项目添加到集合中时,一切似乎都正常工作,控件不会重新绘制以反映添加到集合中的内容。
当我在我的测试表单上单击控件外部时,它会被重新绘制。 通常使用简单的属性我会使用 Invalidate,但这在这里似乎是不可能的。 我还尝试了除 List 之外的其他集合类型,它允许有一个 set 访问器,但仍然不会调用 Invalidate。我认为这意味着永远不会调用 SET。
我知道如何让它与可扩展属性一起使用,但我没有找到如何使用集合进行此更新。
我希望有人可以帮助我。 提前致谢。
【问题讨论】:
-
下次请相应地标记问题。
-
这和不提高收藏有什么关系吗?我想设计师仍然使用组件模型并且没有 collectionchanged 通知,控件不知道内部有任何更改 - (免责声明:这个答案可能是谎言!)
-
PaintEventArgs 是 WinForms,我相信。
-
@Felix KLing - 对不起。我试图添加标签,但我想我太快了:)
-
@Bob - 这是一个winform自定义控件
标签: c# winforms collections properties