简体   繁体   中英

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.

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.

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. I also tried with other collection types than List<> where it is allowed to have a set accessor, but Invalidate still wont be called. I assume that it means that the SET is never called.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM