PropertyGrid disable add/remove buttons in collections

Grids for WPF Forum

Posted 6 years ago by David Garcia
Version: 18.1.0672


We are showing some collections into the PropertyGrid. We are able to add and remove items, and we want to know if it is possible to disable the add or remove buttons... 

Use case 1: Imagine that we have a max and min limits in the collection (ie: min 4 - max 24)... Is there some way to achieve this goal? 

Use case 2: We are sending a command to a server, then we want to disable the add until the feeback arrives to the client.



Comments (1)

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

Hi David,

The property models are what control the add/remove button enabled states and handle their click result via commands.

Our default PropertyGrid value cell template has a ContentPresenter and two Buttons, one for add and one for remove. The buttons are normally hidden unless the value is an editable list (add button shows) or an editable list item (remove button shows).  Technically, the add button is visible if the property model's CanAddChild property is true, and the remove button is visible if the property model's CanRemove property is true.  The buttons bind their Command to the AddChildCommand and RemoveCommand properties on the property model. These two commands call related virtual AddChild and Remove methods when they are executed.

Thus if you override the property model's CanAddChild and CanRemove properties, you have control over whether those buttons are visible.  Note that if you are changing the state of that property, you would need to fire off an INotifyPropertyChanged notification for the property.  And you might need to do it on the related command (AddChildCommand, RemoveCommand) as well.

The trick is getting to a spot where you can override the property models since that gets complex with collections.  The add functionality is controlled by the property model for the collection itself, which is created in the TypeDescriptorFactory.CreateCollectionPropertyModel method:

protected virtual IPropertyModel CreateCollectionPropertyModel(object target, PropertyDescriptor propertyDescriptor, IDataFactoryRequest request) {
	var isEditableInline = (request != null) && (request.CollectionPropertyDisplayMode == CollectionPropertyDisplayMode.EditableInline);
	return new CollectionPropertyDescriptorPropertyModel(target, propertyDescriptor, !isEditableInline, false);

For that you could make a class that inherits CollectionPropertyDescriptorPropertyModel and return it instead with overrides of CanAddChild, etc.

The CollectionPropertyDescriptorPropertyModel.CreateExpandableCollectionConverter is where a converter is created that is capable of generating property descriptors for items based on the collection type.  For a normal editable list, a ExpandableCollectionConverter.ListItemPropertyDescriptor might be generated for an item.  Back in the TypeDescriptorFactory, you could look for CreatePropertyModel being called with that kind of property descriptor and return a custom PropertyDescriptorPropertyModel-inherited class instance instead, one that overrides CanRemove.

That is how you'd effectively get to the override points where you could add your logic.

Actipro Software Support

The latest build of this product (v24.1.2) 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.