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

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

问题描述

我有一个这样定义的ViewModel:

I have a ViewModel defined like this:

public class LocationTreeViewModel<TTree> : 
    ObservableCollection<TTree>, INotifyPropertyChanged
        TTree : TreeBase<TTree>

我想在XAML中的DataTemplateDataType属性中引用它.我该怎么办?

I want to reference it in the DataType attribute of a DataTemplate in XAML. How can I do that?

推荐答案

我能做到这一点的唯一方法是使用MarkupExtensions.

The only way i could do this is to use MarkupExtensions.

public class GenericType : MarkupExtension
{
     private readonly Type _of;
     public GenericType(Type of)
     {
         _of = of;
     }
     public override object ProvideValue(IServiceProvider serviceProvider)
     {
         return typeof(LocationTreeViewModel<>).MakeGenericType(_of);
     }
}

要使用它,我只需要这样做:

And to use it i just need to do this:

<DataTemplate DataType="{app:GenericType app:TreeBaseClass}">

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

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