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

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

问题描述

我有一个 MyClass 类,并希望将其设置为 HierarchicalDataTemplate 的 DataType.

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

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

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 的方法是一个很好的方法,但它只是一个开始.以下是适用于您的案例(以及大多数(如果不是全部)案例)的内容:

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;
    }
}

然后,您可以在 XAML 中创建具有任何深度级别的任何类型.例如:

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:

  • 必须使用标准的` 表示法指定泛型类型.因此,System.Collections.Generic.List<>System.Collections.Generic.List`1.字符 ` 表示该类型是泛型的,其后面的数字表示该类型具有的泛型参数的数量.
  • x:Type 标记扩展能够非常轻松地检索这些基本泛型类型.
  • 泛型参数类型作为 Type 对象数组传递.然后将此数组传递到 MakeGenericType(...) 调用中.
  • 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 的 DataType 属性中引用泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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