如何引用泛型类型的HierarchicalDataTemplate的数据类型属性? [英] How to reference a generic type in the DataType attribute of a HierarchicalDataTemplate?

查看:319
本文介绍了如何引用泛型类型的HierarchicalDataTemplate的数据类型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类 MyClass的<为MyObject> ,并希望将其设置为数据类型为HierarchicalDataTemplate

什么是XAML的语法呢? (我知道如何设置的命名空间,我需要的只是语法

 < HierarchicalDataTemplate数据类型={X:类型.....
 

解决方案

itowlson的做法是一个很好的,但它仅仅是一个开始。这里的东西,将你的情况下工作(大多数,如果不是全部,例):

 公共类GenericType:的MarkupExtension
{
    公共类型基本类型{获取;组; }
    公共类型[] InnerTypes {获得;组; }

    公共GenericType(){}
    公共GenericType(类型基本类型,PARAMS类型[] innerTypes)
    {
        BASETYPE =基本类型;
        InnerTypes = innerTypes;
    }

    公众覆盖对象ProvideValue(的IServiceProvider的ServiceProvider)
    {
        类型结果= BaseType.MakeGenericType(InnerTypes);
        返回结果;
    }
}
 

然后,你可以创建任何类型的,在你的XAML任意的深度。例如:

 < Grid.Resources>
        < X:数组类型={X:键入sys:类型}
                 X:关键=TypeParams>
            < X:类型类型名=SYS:的Int32/>
        < / X:阵列>

        <地方:GenericType BASETYPE ={X:类型类型名=科尔:List`1}
                           InnerTypes ={的StaticResource TypeParams}
                           X:关键=ListOfInts/>

        < X:数组类型={X:键入sys:类型}
                 X:关键=DictionaryParams>
            < X:类型类型名=SYS:的Int32/>
            <地方:GenericType BASETYPE ={X:类型类型名=科尔:List`1}
                               InnerTypes ={的StaticResource TypeParams}/>
        < / X:阵列>

        <地方:GenericType BASETYPE ={X:类型类型名=科尔:Dictionary`2}
                           InnerTypes ={的StaticResource DictionaryParams}
                           X:关键=DictionaryOfIntsToListOfInts/>
    < /Grid.Resources>
 

有几个关键的想法在这里:

  • 在泛型类型必须使用标准的'符号指定。因此, System.Collections.Generic.List<> System.Collections.Generic.List`1 。字符`表示该类型是通用的和之后的数字表示通用参数的类型具有的数目。
  • 的X:类型标记扩展能够很容易地检索这些基地一般类型
  • 的泛型参数类型传递类型对象的数组。这个数组,然后传递到MakeGenericType(...)调用。

I have a class of MyClass<MyObject> and want to set it as the DataType for a HierarchicalDataTemplate.

What is the syntax for this in XAML? (I know how to set namespaces, I need just the syntax for

<HierarchicalDataTemplate DataType="{X:Type .....

解决方案

itowlson's approach is a good one but it is just a start. Here's something that will work for your case (and most, if not all, cases):

public class GenericType : MarkupExtension
{
    public Type BaseType { get; set; }
    public Type[] InnerTypes { get; set; }

    public GenericType() { }
    public GenericType(Type baseType, params Type[] innerTypes)
    {
        BaseType = baseType;
        InnerTypes = innerTypes;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        Type result = BaseType.MakeGenericType(InnerTypes);
        return result;
    }
}

Then, you are able to create any type with any level of depth in your XAML. For example:

    <Grid.Resources>
        <x:Array Type="{x:Type sys:Type}" 
                 x:Key="TypeParams">
            <x:Type TypeName="sys:Int32" />
        </x:Array>

        <local:GenericType BaseType="{x:Type TypeName=coll:List`1}" 
                           InnerTypes="{StaticResource TypeParams}"
                           x:Key="ListOfInts" />

        <x:Array Type="{x:Type sys:Type}" 
                 x:Key="DictionaryParams">
            <x:Type TypeName="sys:Int32" />
            <local:GenericType BaseType="{x:Type TypeName=coll:List`1}" 
                               InnerTypes="{StaticResource TypeParams}" />
        </x:Array>

        <local:GenericType BaseType="{x:Type TypeName=coll:Dictionary`2}"
                           InnerTypes="{StaticResource DictionaryParams}"
                           x:Key="DictionaryOfIntsToListOfInts" />
    </Grid.Resources>

There's a few key ideas here:

  • A generic type has to be specified using the standard ` notation. So, System.Collections.Generic.List<> is System.Collections.Generic.List`1. The character ` indicates that the type is generic and the number after it indicates the number of generic parameters the type has.
  • The x:Type markup extension is able to retrieve these base generic types quite easily.
  • The generic parameter types are passed as an array of Type objects. This array is then passed into the MakeGenericType(...) call.

这篇关于如何引用泛型类型的HierarchicalDataTemplate的数据类型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆