Property Grid and Collection Class

Hi,

I have a Collection class (derived from Generic List class. I have to override as I need to overload some functionalities.) which I am binding to Property Grid. When I do this I get a Collection Type Editor Provided by the MSFT and clicking on which I can add elements to my Collection class. Till this everything is fine. The issue is when I add some thing to my collection I want to Raise a custom event. As I read some wonderfull article (MSDN Mag) which said try using Generic Collection which gives better performance, my collection class is deriving from Generic Collection. But I am not able to override the Add method as it is no more virtual class in Generic List Collection. If I just put a new keyword and try implementing the Add method in my class still it doesn't work. Please suggest me, should I rollback to CollectionBase class instead of List Generic class or I have to do something else.

Regards,

Vinod S Nair

[1136 byte] By [Vinod.S.Nair] at [2008-1-8]
# 1

Hi,

Something more to add. I found the base class for Generic collection (System.Collection.ObjectModel.Collection<T>) and now I am able to override InsertAt RemoveAt and Clear methods. Now the issue is whenever the user adds a new item in the collection it calls the Clear first and redo the add for all the elements. So my notification will go wrong as I have to raise the event only ones for the newly added event.

Do anyone come across this kind of scenario, can you please suggest me how to proceed...

Regards,

Vinod S Nair

Vinod.S.Nair at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

Hi Vinod,

Could you please show your overrided methods in more details?

To troubleshoot this issue, we really need the source code to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.

Thanks!

FengChen-MSFT at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Hi,

I listing the code below.

Code Snippet

public class ItemCollection : Collection<Item>

{

protected override void InsertItem(int index, Item item)

{

base.InsertItem(index, item);

if (item.IsNewItem)

{

RaiseItemAddedEvent(item);

item.IsNewItem = false;

}

}

protected override void SetItem(int index, Item item)

{

base.SetItem(index, item);

}

protected override void RemoveItem(int index)

{

Item i = this[index];

base.RemoveItem(index);

RaiseItemRemovedEvent(i);

}

/// <summary>

/// Method will raise the ItemAdded Event.

/// </summary>

/// <param name="model">Added Item</param>

protected void RaiseItemAddedEvent(IItemObject addedItem)

{

if (ItemAdded != null)

{

ItemAddedEventArgs e = new ItemAddedEventArgs(addedItem);

ItemAdded(this, e);

}

}

/// <summary>

/// Method will raise the RaiseItemRemovedEvent Event

/// </summary>

/// <param name="model">Removed Item</param>

protected void RaiseItemRemovedEvent(IItemObject removedItem)

{

if (ItemRemoved != null)

{

ItemRemovedEventArgs e = new ItemRemovedEventArgs(removedItem);

ItemRemoved (this, e);

}

}

}

public class Consumer

{

private ItemCollection m_ConsumerItems;

public ItemCollection ConsumerItems

{

get {return m_ConsumerItem; }

set { m_ConsumerItems = value; }

}

public Consumer()

{

m_ConsumerItems = new ItemCollection();

}

}

In the above given Example code I have a custom collection class ItemCollection which is derived from Collection<Item>. I am overriding InsertItem, SetItem, RemoveItem, and Clear methods. As you can see the Consumer class has property which is of type ItemCollection. An object of this Consumer is bound to Property Grid. And in the Property grid I get the Default Type Descriptor for collection provided by .Net is shown When I click on the edit button in property grid I can add a new object of Item and Delete one that is fine, But My issue is whenver this happens I have to raise a custom event so that other parts of my application which are intersted in this change will handle this. Annd the actual issue comes now when I use MS provided Default Type Decriptor when I add a new Item it calls the Clear method first and re add the old message and then add the new one. This I handled in an Ugly way you can make out from the code. When the user deletes an Item from the Collection I don't know how to raise the event?

I guess there will be a better way. May I am missing something somewhere...

Regards,

Vinod S Nair

Vinod.S.Nair at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

Hi Vinod.S.Nair,

Your code is incomplete for reproducing the problem.

And I’m not sure about the exactly means of your words: “Annd the actual issue comes now when I use MS provided Default Type Decriptor when I add a new Item it calls the Clear method first and re add the old message and then add the new one.” And “This I handled in an Ugly way you can make out from the code.” So could you please clarify?

And I’m not sure about how do you know that “Decriptor when I add a new Item it calls the Clear method first and re add the old message and then add the new one”?

Thanks!

FengChen-MSFT at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Hi Vinod.S.Nair,

We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Thank you!

FengChen-MSFT at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified