Custom PropertyDescriptorDataAccessor for Collection Items

Grids for WPF Forum

Posted 2 years ago by Darcy Davidson
Version: 16.1.0632
Avatar

Hi,

I was wondering how to create my own PropertyDescriptorDataAccessor for collection items.  They seem to get created behind the scenes and I can't override them in the TypeReflectionFactory.

Thanks.

ps - we will be moving to 2017.2 shortly in order to take advantage of performance improvements of new property grid.  As much as the old one has served us well, lets just say a faster version will be very welcome.  We have heavily customized the old one, so hopefully the port isn't too painful.

Comments (11)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Darcy,

For the 2016.1 version, yes collection items are a bit tricky because they need to be created by an ExpandableCollectionConverter instance, which is set to the PropertyGrid.CollectionConverter property.  In that converter, we override the GetProperties method and return generated property descriptors.  There are methods you can override like CreateListPropertyDescriptor if you wish to tweak anything for what is created.

When you convert over to 2017.1, be sure to read through the documentation topic on "Converting to 2017.1".  That gives a lot of information about breaking changes you will encounter.  You can always ask us for help if something isn't clear too.


Actipro Software Support

Posted 2 years ago by Darcy Davidson
Avatar

Thanks,

I am still stuck.  Here's what I want to accomplish:

I have a readonly collection/list/array of objects.  I want each enumerable item to show as a category header (or something that looks/feels like a category header), with each object property for the corresponding item to be nested within the category.

As a workaround, I was trying to use your advice above to override the [0], [1]....[N] text in the collection item list, but I wasn't able to succeed (would still like to know how to do this).

Questions:

- What should I use for the collection/list/arrary, and what attributes does it need (if any)?

- Can you give me some pointers as to what I need to do to accomplish the above requirement?

Thanks,

D'Arcy

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Darcy,

In order to change the visual property editors that are rendered for each item, you would need to probably override the TypeDescriptorFactory.GetProperties method on the factory you assign to PropertyGrid.  That method is passed a "value," which could be your readonly collection or other things and returns the IPropertyDataAccessors that should be children of it.

We will generate the property data accessors for items by default.  You could perhaps get the base method results, and if the "value" is the readonly collection type, tweak those results to be what you need.  Perhaps you make a wrapper IPropertyDataAccessor that adjusts the DisplayName to be something other than index numbers, but otherwise provides the wrapped IPropertyDataAccessor's properties exactly.  That would allow you to keep everything populating more or less as-is but to coerce the display name of the collection item properties.


Actipro Software Support

Posted 2 years ago by Darcy Davidson
Avatar

We have decided to move to the new property grid.  I am a few days into the port and have many things working.  The new architecture is much simpler.  I have one question, and one issue.

Question:  Why is SortOrder readonly in PropertyDescriptorPropertyModel and CollectionPropertyDescriptorPropertyModel, yet read/write in CategoryModel?  It makes me think I am missing something about its intented use.

Issue: I am trying to attach visibility control to categories, such that if I dynamically hide all of a categories' children the category itself hides as well.  It is all working as expected, except I can't figure out how to add a setter to the existing category style:

<grids:PropertyGridItemStyleSelector.CategoryStyle >
<Style TargetType="grids:PropertyGridItem" >
<Setter Property="Visibility" Value="{Binding IsVisible, Converter={x:Static local:VisibilityConverterCollapsed.Instance}}" />
</Style>
</grids:PropertyGridItemStyleSelector.CategoryStyle>

I want to base it on the existing style, but can't find it.  I also tried adding the setter in code by creating a new style based on DefaultCategoryItemContainerStyle and setting the new style back, but that throws an exception.

Thanks,

D'Arcy

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi D'Arcy,

The PropertyDescriptorPropertyModel class retrieves the sort order from a DisplayAttribute on the property with the Order attribute property set.  If you decorate your property like this, you can control sort order:

[Display(Order = 3)]

In the case of CategoryModel, there is no backing property for that so it needs to be settable.

For the style, you'd need to copy the Style out of our Grids.xaml file if you download the default styles and templates.  You'd have to write our support address for those since you aren't a WPF Studio customer.  But really, you can probably do this in the data factory instead.  If you create a class that overrides our TypeDescriptorFactory.CategorizeDataModels method, you could call the base method to categorize everything.  Then scan the results of that and remove any ICategoryModel instances that are empty.

[Modified 2 years ago]


Actipro Software Support

Posted 2 years ago by Darcy Davidson
Avatar

Thanks for the quick reply.  I was missing the attribute connection on the read only properties...I have my own sorting attribute which I use so I derived my own version of the 'propertydescriptor' classes and overrode the SortOrder properties.....works fine, but had I known there was a built-in way of doing it I probably would have used that.

As for the style, I did exactly what you suggested in CategorizeDataModels while I was waiting for your response.  After categorization, I recurse the tree, refreshing dynamic visibility properties and removing unwanted categories as I go.  Works well, thank you.

Posted 2 years ago by Darcy Davidson
Avatar

Hi,

How do I set the default container style for collections? (The header)

Thanks,

D'Arcy

[Modified 2 years ago]

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi D'Arcy,

Can you clarify what you are trying to customize?  Do you mean the value cell's template content for an expandable collection like where it sometimes says the number of items, depending on the collection type and display mode?  For instance in our "Collection New Items" QuickStart, where it says "3 items".  Is that what you are trying to change, and to what?


Actipro Software Support

Posted 2 years ago by Darcy Davidson
Avatar

Hi,

I accomplished what I was trying to do at the moment:  I don't like how a non-empty collection shows the expander at the same indent as the category it is a member of (it visually appears like another category at the same level).  I created an attached behavior on the PropertyGridItem to modify 'IndentAmount' such that when the expander is visible, the collection header (and sub items) get indented another increment.

Thanks,

D'Arcy

Posted 2 years ago by Darcy Davidson
Avatar

New question/issue (you'll be happy to hear I am almost done the port:)

I am overriding the dispose in PropertyDescriptorPropertyModel to clean up an event handler I have added, but it never gets hit.  Is there another way to do this?

Thanks,

D'Arcy

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi D'Arcy,

It should be cycling through and disposing the old property models whenever the property grid refreshes (i.e. a DataObject change).  I just did a breakpoint in our code and it seemed to be working.

If you don't see that happening, please make a new simple sample project that shows it and email that to our support address, mentioning this thread.   Be sure to remove any bin/obj folders from the ZIP you send and rename the .zip file extension so it doesn't get spam blocked.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.