I am attempting to specify a KeyValuePair<int,MyClass>
as the data type for a DataTemplate in WPF. The KeyValuePair is from an ObservableDictionary
object (I am using Dr WPF's implementation if it is relevant.
Using How to reference a generic type in the DataType attribute of a HierarchicalDataTemplate?, I was able to get something close, however, I am getting a compiler error.
I need to be able to define the HierarchicalDataTemplate for the KeyValuePair so I can create another leaf in the TreeView below it containing a collection that is part of the Value object of the Dictionary.
The XAML is simply:
<HierarchicalDataTemplate DataType="{local:GenericKeyValuePairType
KeyType=sys:Int32,
ValueType=local:MyClass}"
ItemsSource="{Binding Path=Value.MyList}" />
And GenericKeyValuePairType is a MarkupExtension
public class GenericKeyValuePairType : MarkupExtension
{
public Type KeyType { get; set; }
public Type ValueType { get; set; }
public GenericKeyValuePairType() { }
public GenericKeyValuePairType(Type keyType, Type valueType)
{
KeyType = keyType;
ValueType = valueType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return typeof(KeyValuePair<,>).MakeGenericType(this.KeyType, this.ValueType);
}
}
When I do this, Visual Studio indicates that this is not valid. The specific error is:
A key for a dictionary cannot be of type 'TreeViewBindingTest.GenericKeyValuePairType'. Only String, TypeExtension, and StaticExtension are supported.
Is what I am trying to do even possible? If so, is there a better approach?