wpf - DataTemplate.DataType = 集合<实体>?

标签 wpf entity-framework datatemplate generic-list generic-programming

有没有办法创建一个处理项目列表的数据模板?

我有 Contact.Phones ( EntityCollection<Phone> ),我希望数据模板处理列表 - 添加删除编辑等。

有没有办法将 DataTemplate 的 DataType 属性设置为通用 EntityCollection<Phone> ?

最佳答案

在我的 Emerald Data Foundation (EDF) 工具中,我通过创建一个比 x:Type 更强大的 MarkupExtension 来解决这个问题,它也可以指定泛型类型。这样我可以写:

 <DataTemplate TargetType="{edf:Type generic:ICollection{local:Entity}}" />

这是我使用的:
  [MarkupExtensionReturnType(typeof(Type))]
  public class TypeExtension : MarkupExtension
  {
    public TypeExtension() { }
    public TypeExtension(string typeName) { TypeName = typeName; }
    public TypeExtension(Type type) { Type = type; }

    public string TypeName { get; set; }
    public Type Type { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
      if(Type==null)
      {
        IXamlTypeResolver typeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
        if(typeResolver==null) throw new InvalidOperationException("EDF Type markup extension used without XAML context");
        if(TypeName==null) throw new InvalidOperationException("EDF Type markup extension used without Type or TypeName");
        Type = ResolveGenericTypeName(TypeName, (name) =>
        {
          Type result = typeResolver.Resolve(name);
          if(result==null) throw new Exception("EDF Type markup extension could not resolve type " + name);
          return result;
        });
      }
      return Type;
    }

    public static Type ResolveGenericTypeName(string name, Func<string, Type> resolveSimpleName)
    {
      if(name.Contains('{'))
        name = name.Replace('{', '<').Replace('}', '>');  // Note:  For convenience working with XAML, we allow {} instead of <> for generic type parameters

      if(name.Contains('<'))
      {
        var match = _genericTypeRegex.Match(name);
        if(match.Success)
        {
          Type[] typeArgs = (
            from arg in match.Groups["typeArgs"].Value.SplitOutsideParenthesis(',')
            select ResolveGenericTypeName(arg, resolveSimpleName)
            ).ToArray();
          string genericTypeName = match.Groups["genericTypeName"].Value + "`" + typeArgs.Length;
          Type genericType = resolveSimpleName(genericTypeName);
          if(genericType!=null && !typeArgs.Contains(null))
            return genericType.MakeGenericType(typeArgs);
        }
      }
      return resolveSimpleName(name);
    }
    static Regex _genericTypeRegex = new Regex(@"^(?<genericTypeName>\w+)<(?<typeArgs>\w+(,\w+)*)>$");

  }

泛型类型名称解析代码在一个单独的方法中,因为它也被 EDF 中的一些其他代码使用。您可以将它们全部组合成一种方法。

关于wpf - DataTemplate.DataType = 集合<实体>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1665267/

相关文章:

c# - OneDrive oauth wpf 禁用自动登录浏览器

c# - linq to entities 无法识别方法

c# - 带有用于搜索的文本框的 DataTemplate

xaml - 对于 longlistselector 项目 (WP8),DataTemplate 中突然无效的 XAML

wpf - TaskScheduler.FromCurrentSynchronizationContext - 单元测试时如何使用 WPF 调度程序线程

c# - 使用 C# 从 UIElement 获取 WPF 屏幕截图 JPG

c# - wpf制作幻灯片动画

c# - 使用 select new 语句返回所有记录的 Linq 查询

c# - 无法在 .NET Core 中使用 EF 更新数据库

c# - 出现 VerticalScrollBar 时调整边框大小