[英]How do I “Invalidate” after adding items to a collection property?
I am creating a Custom control in where I am creating a property of the type "List" 我正在创建一个自定义控件,我在其中创建“列表”类型的属性
Sections is a public class which has 4 properties. Sections是一个公共类,有4个属性。
The code in the control looks as follows: 控件中的代码如下所示:
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.
}
}
The Sections Class is as follows: 章节类如下:
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;
}
}
}
Everything seems to work fine except that when I add items to the collection at designtime using the property browsers typeeditor the control is not repainted to reflect what is added to the collection. 一切似乎都运行良好,除了当我在设计时使用属性浏览器typeeditor向集合添加项目时,控件不会重新绘制以反映添加到集合中的内容。
When I click outside the control on my testform it is repainted. 当我点击我的测试表格上的控件外面时,它被重新绘制。 Usually with simple properties I would use Invalidate, but this seems not to be possible here. 通常使用简单属性我会使用Invalidate,但这似乎不可能在这里。 I also tried with other collection types than List<> where it is allowed to have a set accessor, but Invalidate still wont be called. 我还尝试使用除List <>之外的其他集合类型,其中允许有一个set访问器,但仍然不会调用Invalidate。 I assume that it means that the SET is never called. 我认为这意味着从未调用过SET。
I know how to get this to work with expandable properties but I have no luck finding how to make this update with collections. 我知道如何使用可扩展属性,但我没有找到如何使用集合进行此更新。
I hope someoone can help me out. 我希望有人可以帮助我。 thanks in advance. 提前致谢。
Instead of using the class List, use the class ObservableCollection, and use that to get notified when a new section is added or removed from the list. 不使用类List,而是使用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();
}
When using your example exactly as it was the Indicators property was not available for editing in the property window. 完全按原样使用您的示例时,属性窗口中的“指标”属性无法进行编辑。 So I made a few changes to it. 所以我做了一些改动。
I added a new class: 我添加了一个新类:
// 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.
}
Then I made the change as you suggested 然后我按你的建议做了改变
public genericGauge()
{
InitializeComponent();
this.indicators.CollectionChanged += this.IndicatorsCollectionChanged; // your suggestion
}
And made the property like this instead: 并使这个属性改为:
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();
}
And now works as a charm. 而现在作为一种魅力。 Thank you very much. 非常感谢你。 I appreciate to see that it IS possible to get help this fast. 我很欣赏能够快速获得帮助。 I like this forum alot. 我很喜欢这个论坛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.