I've noticed a strange behavior of the PropertyGrid in combination with a BindingList.
When adding an item to the BindingList, the PropertyGrid first clears all items out of the BindingList and then just add all previous items again one by one and only afterwards it adds the new item to it.
My problem is, it raises the 'ListChanged' event every time but i just want to have it once for each added item not for example 3 times when adding a second item.
Here is an example:
private void Form1_Load(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = new Foo();
}
private class Foo
{
[DisplayName("BindingList")]
[Description("BindingList")]
[Category("BindingList")]
[Browsable(true)]
public BindingList<int> Bar { get; set; } = new BindingList<int>();
public int BindingListEventRaiseCount { get; set; } = 0;
public Foo(){
Bar.ListChanged += OnBinListChanged;
}
private void OnBinListChanged(object sender, EventArgs e){
BindingListEventRaiseCount++;
Console.WriteLine("Current ListElementsCount: " + ((BindingList<int>)sender).Count);
}
}
Output while adding 3 items through the propertyGrid control:
Current ListElementsCount: 0
Current ListElementsCount: 1
Current ListElementsCount: 0
Current ListElementsCount: 1
Current ListElementsCount: 2
Current ListElementsCount: 0
Current ListElementsCount: 1
Current ListElementsCount: 2
Current ListElementsCount: 3
Is there a fix for this bug?
I don't know if that helps but there is always a Reset
before the items are created again.
When pressing Ok, a reset is also performed.
private void OnBinListChanged(object sender, ListChangedEventArgs e)
{
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
BindingListEventRaiseCount++;
Console.WriteLine("Current ListElementsCount: " + ((BindingList<int>)sender).Count);
break;
case ListChangedType.Reset:
Console.WriteLine("Reset");
break;
}
}
Output (for 4 items):
Reset
Current ListElementsCount: 1
Reset
Current ListElementsCount: 1
Current ListElementsCount: 2
Reset
Current ListElementsCount: 1
Current ListElementsCount: 2
Current ListElementsCount: 3
Reset
Current ListElementsCount: 1
Current ListElementsCount: 2
Current ListElementsCount: 3
Current ListElementsCount: 4
Reset
Current ListElementsCount: 1
Current ListElementsCount: 2
Current ListElementsCount: 3
Current ListElementsCount: 4