How to reference a generic type in the DataType attribute of a DataTemplate?

后端 未结 8 1428
深忆病人
深忆病人 2020-12-18 18:19

I have a ViewModel defined like this:

public class LocationTreeViewModel : 
    ObservableCollection, INotifyPropertyChanged
               


        
8条回答
  •  暖寄归人
    2020-12-18 19:00

    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:

    
    

提交回复
热议问题