Replace PropertyEditor for Expandable Collection properties

Grids for WPF Forum

Posted 4 months ago by Tom P.
Version: 22.1.0
Avatar

I can't seem to find the default PropertyEditor for Collections.  I need to change the look and behavior of these properties.

Specifically, I need to override the way the + button looks and works. I realize I can inherit from ExpandableCollectionConverter and override CreateItem().  But changing the look isn't obvious to me.

Where is the default PropertyEditor for Collections? I want to make sure I retain the expansion capability.

Thanks!

Tom

Comments (4)

Posted 4 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tom,

The +/- buttons are defined in the PropertyGrid.ValueContainerStyle property that is used on all value cells.  It's not specific to collections, although that's really the only place they are used.  That property defaults to this:

<Setter Property="ValueContainerStyle">
	<Setter.Value>
		<Style TargetType="grids:TreeListViewItemCell">
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="grids:TreeListViewItemCell">
						<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
								SnapsToDevicePixels="true">
							<Grid>
								<Grid.ColumnDefinitions>
									<ColumnDefinition Width="*" />
									<ColumnDefinition Width="Auto" />
									<ColumnDefinition Width="Auto" />
								</Grid.ColumnDefinitions>
										
								<ContentPresenter Margin="{TemplateBinding Padding}" />
										
								<Button Grid.Column="1" CommandParameter="{Binding}" Command="{Binding AddChildCommand}"
										ToolTip="{products:SRExtension UIPropertyGridAddChildButtonToolTip}"
										Visibility="{Binding Path=CanAddChild, Converter={StaticResource BooleanToVisibilityConverter}}"
										Style="{StaticResource EmbeddedButtonStyle}">
									<Button.ContentTemplate>
										<DataTemplate>
											<Canvas Width="7" Height="7" Opacity="0.4">
												<Path Stroke="{Binding RelativeSource={RelativeSource Self}, Path=(TextElement.Foreground)}"
														Data="M 0,3.5 L 7,3.5 M 3.5,0 L 3.5,7" />
											</Canvas>
										</DataTemplate>
									</Button.ContentTemplate>
								</Button>
										
								<Button Grid.Column="2" CommandParameter="{Binding}" Command="{Binding RemoveCommand}"
										ToolTip="{products:SRExtension UIPropertyGridRemoveButtonToolTip}"
										Visibility="{Binding Path=CanRemove, Converter={StaticResource BooleanToVisibilityConverter}}"
										Style="{StaticResource EmbeddedButtonStyle}">
									<Button.ContentTemplate>
										<DataTemplate>
											<Canvas Width="7" Height="7" Opacity="0.4">
												<Path Canvas.Top="3" Stroke="{Binding RelativeSource={RelativeSource Self}, Path=(TextElement.Foreground)}"
														Data="M 0,0.5 L 7,0.5" />
											</Canvas>
										</DataTemplate>
									</Button.ContentTemplate>
								</Button>
							</Grid>
						</Border>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</Setter.Value>
</Setter>

You can clone the Style above and adjust it as needed.  Then set your updated Style as the property value.


Actipro Software Support

Posted 4 months ago by Tom P.
Avatar

Ah, that is fascinating! 

So my next question is maybe more to the point.

My collection can have at least 2 different types in it: let's call them MyType and MyBase. MyType inherits from MyBase. The collection type is say List<MyBase>. So of course, clicking the + just creates a MyBase.

Ideally, I want to replace the + with ... and that would pop up a menu with my 2 options like: Add MyType or Add My Base

I can certainly attempt this via an override of ExpandableCollectionConverter::CreateItem(...) but there is no real context for the popup menu from there.

If it was possible to specify a PropertyEditor for IList (for example) then I I could just use that mechanism. Do you see where I am going? I tried it, but I completely lost the ability to show the child items automatically. This is why I was asking for the PropertyEditor for Collections :).

Posted 4 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tom,

Ahh I see, you want to display a menu to choose which type of object to create.  Right now the template above is using IPropertyModel.AddChildCommand (which ends up calling AddChild() in its Execute and CanAddChild for its CanExecute) to add the new child.  But as you said, it has no context of the UI element that called it.

We are open to any suggestions you have for API enhancements, although breaking changes would likely need to wait until a major release.

I wonder if instead, you could update the template above to use an inherited shared:PopupButton for the add button instead.  Override the PopupButton.OnPopupOpening method to dynamically build a PopupMenu like we do in the OnDynamicPopupButtonPopupOpening method of our PopupButton QuickStart.  You could show your type options there and update things appropriately in response to a menu item click.

[Modified 4 months ago]


Actipro Software Support

Posted 4 months ago by Tom P.
Avatar

First, thank you as always for your prompt support. Blows my mind every time.

I wonder if instead, you could update the template above to use an inherited shared:PopupButton for the add button instead.  Override the PopupButton.OnPopupOpening method to dynamically build a PopupMenu like we do in the OnDynamicPopupButtonPopupOpening method of our PopupButton QuickStart.  You could show your type options there and update things appropriately in response to a menu item click.

Good suggestion. I do think however that a little more control over the way Collection properties are rendered via a PropertyEditor could be more expressive. For example, I could also add  2 buttons instead of the single plus, each with an icon representing the type instead of a menu. This is faster to use than a context menu, but, your point about breaking changes is understood. 

Not urgent at this time. I may end up utilizing the TreeGridListView myself for a PropertyGrid like UI that has been requested, but is more customized than I suspect I can get with the PropertyGrid itself.

The latest build of this product (v22.1.2) was released 1 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.