Does a derived Dictionary really require the NewItemTypesAttribute to work with the Xceed CollectionEditor?

123 views Asked by At

I am using the PropertyGrid control (and by extension the CollectionEditor) from the Extended WPF Toolkit 3.0 Community Edition. It does a great job most of the time, but there seems to be a small difference in behavior when having a plain Dictionary declared in your class compared to using a custom class that extends Dictionary.

Take the following example class:

Public Class TestObject
    Public Property MyDictionary As New Dictionary(Of String, String)
End Class

and this XAML and code-behind to show it in the PropertyGrid:

<Window x:Class="MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
         xmlns:local="clr-namespace:myApp"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <xctk:PropertyGrid x:Name="myPropertyGrid"/>
    </Grid>
</Window>

Public Class MainWindow
    Public Sub New()
        InitializeComponent()
        myPropertyGrid.SelectedObject = New TestObject()
    End Sub
End Class

The result will look like this and everything is fine: CollectionEditor with EditableKeyValuePair`2


However, if I now change the definition from

Public Property MyDictionary As New Dictionary(Of String, String)

to

Public Property MyDictionary As New DerivedDictionary(Of String, String)

where DerivedDictionary can be simply declared as

Public Class DerivedDictionary(Of TKey, TValue)
    Inherits Dictionary(Of TKey, TValue)
End Class

then I will no longer get an EditableKeyValuePair`2 presented in the combobox of the CollectionEditor, but instead just a KeyValuePair`2, which (as you may have guessed) is not editable. Adding elements to the DerivedDictionary from the code-behind will make them show up as EditableKeyValuePair`2 in the editor as well, so the only problem is the addition of new elements via the UI.

I know that I could solve the issue by adding

<NewItemTypes(GetType(EditableKeyValuePair(Of String, String)))>

to the declaration of MyDictionary in TestObject. But this seems pretty verbose if I had to do it for every case where a DerivedDictionary is used by a class that is meant to be editable via the UI.

Am I missing an easier alternative here? Maybe some attribute that I need to apply to DerivedDictionary to make it work? Or is that a bug/limitation in the current PropertyGrid control?

0

There are 0 answers