This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi I have a question... I have a class which is derive from ObservableCollection<item>. class MyClass : ObservableCollection<Item>
How is the best approach for that. Thanks before. |
|
|
That's a bit of a tricky one. We deliberately do not show properties of collection objects (that is, any object that implements IList or IDictionary) because we don't want spurious properties such as Count appearing in the grid. One possible workaround is to implement ICustomTypeDescriptor on MyClass, but this will require you to synthesise pseudo-properties for the collection elements (e.g. separate Item1, Item2 and Item3 properties) (because PropertyDescriptor doesn't support indexed properties), and will deny you the add/remove behaviour. If this is not acceptable then we will need to tweak the grid to allow properties of collection types to "opt in" to being displayed. Let us know if you need us to do this. |
|
|
Can you provide a simple example using ICustomTypeDescriptor please. Thanks. |
|
|
Well, you should implement all methods from ICustomTypeDescriptor, but for most of them you would just delegate to the equivalent TypeDescriptor method. For example: AttributeCollection ICustomTypeDescriptor.GetAttributes() The only substantive implementations are the GetProperties() methods, and your implementation will look something like this: public PropertyDescriptorCollection GetProperties() This needs a helper class which I have called IndexedPropertyDescriptor which represents the "fake" properties that wrap the collection items: private class IndexedPropertyDescriptor : PropertyDescriptor (I know this looks bloaty but most of it is abstract class implementation boilerplate.) A couple of caveats on this: * It is not currently responsive to changes made to the collection through code. That is, it will not raise PropertyChanged events for the synthesised ItemX properties. (And the property grid isn't smart enough to marry up the CollectionChanged events to the synthesised properties.) Similarly, if things get added to or removed from the collection, that won't cause the type descriptor to update. * As previously mentioned, the grid will no longer see this as a collection (because the custom type descriptor takes priority), and therefore won't offer add/remove UI. * As indicated in the comments, I have skipped propagating attributes from the real properties to the PropertyDescriptors. In a real implementation you would probably want to address this. So this may not be suitable for your requirements, but it should provide a basis for evaluating the approach. If the approach isn't suitable, let us know if not and we will see what we can do. |
|
|
Previously I used Now I tried
|
|