DataTemplates and generic types

三世轮回 提交于 2019-12-20 03:49:09

问题


I have a generic class

public abstract class BaseViewModel<T>

Since I am trying to create a DataTemplate that will be applied to all classes that derive from BaseViewModel, I set the type to BaseViewModel:

<DataTemplate DataType="{x:Type vm:BaseViewModel}">

However, this does not work since it looks like xaml does not support generic data types.

Are there any work arounds to this?


回答1:


I would create a non-generic version of BaseViewModel and let the generic one inherit from it:

public abstract class BaseViewModel
{
    // members that are not T-specific, if any
    // (not required, but could prove useful)
}

public abstract class BaseViewModel<T> : BaseViewModel
{
    // T-specific members
}

By doing this, your DataTemplate is going to work.



来源:https://stackoverflow.com/questions/6586528/datatemplates-and-generic-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!