C# “我该怎么做?”;“作废”;将项目添加到集合属性后?
我正在创建一个自定义控件,其中我正在创建“List”类型的属性 Sections是一个具有4个属性的公共类 控件中的代码如下所示:C# “我该怎么做?”;“作废”;将项目添加到集合属性后?,c#,winforms,collections,properties,C#,Winforms,Collections,Properties,我正在创建一个自定义控件,其中我正在创建“List”类型的属性 Sections是一个具有4个属性的公共类 控件中的代码如下所示: public partial class genericGauge : Control { public genericGauge() { InitializeComponent(); } // Stripped out code not needed for this issue q
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.
}
}
除了在设计时使用属性浏览器typeeditor将项添加到集合中时,控件没有重新绘制以反映添加到集合中的内容之外,一切似乎都正常工作
当我在testform上的控件外单击时,它将被重新绘制。
通常对于简单的属性,我会使用Invalidate,但这在这里似乎是不可能的。
我还尝试了除List之外的其他集合类型,其中允许有一个set访问器,但仍然不会调用Invalidate。我假设这意味着永远不会调用集合
我知道如何让它与可扩展属性一起工作,但我没有运气找到如何对集合进行此更新
我希望有人能帮助我。
提前感谢。不要使用类列表,而是使用类ObservableCollection,并使用它在列表中添加或删除新节时获得通知
private ObservableCollection<Sections> indicators = new ObservableCollection<Sections>();
public IList<Sections> Indicators
{
get
{
return indicators;
}
}
public genericGauge()
{
InitializeComponent();
this.indicators.CollectionChanged += this.IndicatorsCollectionChanged;
}
private void IndicatorsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// possibly inspect the NotifyCollectionChangedEventArgs to see if it's a change that should cause a redraw.
// or not.
this.Invalidate();
}
私有ObservableCollection指示符=新ObservableCollection();
公共IList指标
{
得到
{
回报指标;
}
}
公共通用仪表()
{
初始化组件();
this.indicators.CollectionChanged+=this.indicatorscolectionchanged;
}
私有无效指示符CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
//可能会检查NotifyCollectionChangedEventArgs,以查看是否是应导致重新绘制的更改。
//或者不是。
这个。使无效();
}
完全按照示例使用时,无法在属性窗口中编辑Indicators属性。所以我对它做了一些修改
我添加了一个新类:
// Added this class to deal with the Sections class
public class SectionObservable : ObservableCollection<Sections>
{
// Added a few methods here for creating a designtime collection if I need to.
}
把房子改成这样:
private SectionObservable indicators = new SectionObservable(); // using the SectionObservable class instead
public SectionObservable Indicators // using the SectionObservable class instead
{
get
{
return indicators;
}
}
private void IndicatorsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) // your suggestion
{
this.Invalidate();
}
现在是一种魅力。
非常感谢你。我很高兴能这么快得到帮助。我非常喜欢这个论坛。下次请相应地标记问题。这与不更改收藏有关吗?我想象设计者仍然使用componentmodel,并且在没有collectionchanged通知的情况下,控件不知道内部有任何更改-(免责声明:这个答案可能是谎言!)我相信PaintEventArgs是WinForms。@Felix KLing-抱歉。我试图添加标签,但我想我太快了:)@Bob-这是一个winform自定义控件,对我非常有用。至少它给了我一个想法。如果我想展示我是如何从你的代码中实现它的,我可以回答我自己的问题吗?如果超过两句话,可以,回答你自己的问题。如果它很短,我会把它放在评论里。好的。我不能回答我自己的问题。我没有足够的声誉:)我会在8小时后发布我的更改,当我被允许回答:)
public genericGauge()
{
InitializeComponent();
this.indicators.CollectionChanged += this.IndicatorsCollectionChanged; // your suggestion
}
private SectionObservable indicators = new SectionObservable(); // using the SectionObservable class instead
public SectionObservable Indicators // using the SectionObservable class instead
{
get
{
return indicators;
}
}
private void IndicatorsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) // your suggestion
{
this.Invalidate();
}