数据模板选择器获取传递对象的类型

本文关键字:对象 类型 获取 选择器 数据 | 更新日期: 2023-09-27 18:37:21

我有一个名为"item"的对象,女巫从XAML传递给方法

这是我在断点上得到的:

base {System.Reflection.TypeInfo} = Name = "Country" Full/Name = "Playground.Domain.Country"}

我正在尝试查找如何通过以下方式找到哪个"类型"是该项目

public class EditorTemplateSelector : DataTemplateSelector
    {
      public override DataTemplate SelectTemplate(object item,
                                                  DependencyObject container)
      {
        DataTemplate template = null;
        var templateName = "NotFoundEditor";
        if (item != null)
        {
          FrameworkElement element = container as FrameworkElement;
          if (element != null)
          {
            if (item is City)
              templateName = "CityEditor";
            else if (item is Country)
              templateName = "CountryEditor";
            template = element.FindResource(templateName) as DataTemplate;
          }
        }
        return template;
      }

但没有运气。

object item从中获取数据

public Type ModelType
{
  get { return typeof(T); }
}

有什么建议吗?

数据模板选择器获取传递对象的类型

根据您上次的编辑:

如果"item"是"System.Type"而不是它的实例,则使用:

   if(item == typeof(City))

你能试着看看你在AppDomain.CurrentDomain.GetAssemblies()中是否有多个"Playground"程序集的实例吗?

如果通过 dll 引用(在"添加引用"对话框中通过"浏览"选择)而不是项目引用从另一个项目引用此程序集,则可能会发生这种情况。

换句话说:当您引用同一程序集的两个不同版本时,会发生这种奇怪的事情。

[编辑] 如果是这样,它与 XAML 无关