派生词典是否真的需要NewItemTypesAttribute才能使用Xceed CollectionEditor?

时间:2016-12-27 15:41:55

标签: wpf vb.net dictionary propertygrid xceed

我正在使用Extended WPF Toolkit 3.0 Community Edition中的PropertyGrid控件(扩展名为CollectionEditor)。它在大多数情况下表现很好,但与使用扩展Dictionary的自定义类相比,在您的类中声明普通Dictionary时,行为似乎存在细微差别。

采用以下示例类:

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

以及此XAML和代码隐藏在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

结果看起来像这样,一切都很好: CollectionEditor with EditableKeyValuePair`2

但是,如果我现在更改

的定义
Public Property MyDictionary As New Dictionary(Of String, String)

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

其中DerivedDictionary可以简单地声明为

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

然后我将不再在EditableKeyValuePair`2的组合框中显示CollectionEditor,而是只显示KeyValuePair`2,(您可能已经猜到)不可编辑。从代码隐藏中向DerivedDictionary添加元素也会使它们在编辑器中显示为EditableKeyValuePair`2,因此唯一的问题是通过UI添加新元素。

我知道我可以通过添加

来解决问题
<NewItemTypes(GetType(EditableKeyValuePair(Of String, String)))>

MyDictionaryTestObject的声明。但是,如果我必须在一个可以通过UI编辑的类使用DerivedDictionary的情况下执行此操作,这似乎相当冗长。

我在这里错过了一个更容易的选择吗?也许某些属性我需要应用于DerivedDictionary才能使其有效?或者这是当前PropertyGrid控件中的错误/限制?

0 个答案:

没有答案