【问题标题】:How to apply a DataTemplate to a XElement subclass?如何将 DataTemplate 应用于 XElement 子类?
【发布时间】:2009-04-19 12:41:49
【问题描述】:

当我从XElement 子类化时,适用于XElementDataTemplate 使用元素名称为DataType,不适用于子类。有什么想法吗?

<HierarchicalDataTemplate DataType="grupo" ItemsSource="{Binding Path=Elements}">
        <TextBlock Text="{Binding Path=Attribute[name]}" />
</HierarchicalDataTemplate>
<!-- When I build an XDocument with XElements like this the template gets applied -->
XDocument _xdoc=XDocument.Load(@"RosterData1.xml");
treeRoster.DataContext = _xdoc;     
<TreeView Name="treeRoster" ItemsSource={Binding Path=Root.Elements}>
</TreeView>  
<!-- but if build de XDocument like this the DataTemplate doesn't apply -->
XDocument _xdoc=XDocument.Load(@"RosterData1.xml");
XDocument docOther = new XDocument(new XElement("contactos"));
var grupos = _xdoc.Descendants("grupo").Select(g => new Grupo(g));
docOther.Root.Add(grupos.ToArray());
var contactos = _xdoc.Root.Elements("contacto").ToList();
docOther.Root.Add(contactos);
treeRoster.DataContext = docOther;  
<!-- The xml file is like that:
<contactos>
  <grupo name="Amigotes">
    <contacto jid="batman@jabber.org" subscription="none" />
    <contacto jid="trucoman@jabber-hispano.org" subscription="both" name="truco" />
    <contacto jid="mmakinavaja@gmail.com" subscription="none" name="mmakinavaja" />
    <contacto jid="ramon@jabber-hispano.org" subscription="both" name="Ramon" />
 </grupo>
 <grupo name="Lannisters">
    <contacto jid="jamie@jabberes.org" subscription="none" />
 </grupo>
 <contacto jid="tyrion@jabber.org" subscription="none" />
   <contacto jid="nslbot@jabber.org" subscription="none" />
   <contacto jid="nslbot@jabberes.org" subscription="none" />
</contactos> -->  

【问题讨论】:

    标签: c# xml linq-to-xml datatemplate xelement


    【解决方案1】:

    我知道它为什么不起作用,但我不明白为什么它是这样实现的。使用反射器查看“FindTemplateResourceInternal”方法中的代码,我找到了答案:

    Type baseType = item.GetType();
    if ((baseType.FullName == "System.Xml.Linq.XElement") && 
    ((obj2 = GetXLinqTagName(item, baseType)) != null))
    {
        baseType = null;
    }
    

    好的,我知道我班级的全名不是System.Xml.Linq.XElement。但后来,一旦你进入了“while”:

    while (obj2 != null)
    {
        object obj3 = null;
        if (templateType == typeof(DataTemplate))
        {
            obj3 = new DataTemplateKey(obj2);
        }
        if (obj3 != null)
        {
            keys.Add(obj3);
        }
        if (exactMatch == -1)
        {
            exactMatch = keys.Count;
        }
        if (baseType != null)
        {
            baseType = baseType.BaseType;
            //HERE baseType FullName is XElement
            //Why don't check it again??
            if (baseType == typeof(object))
            {
                baseType = null;
            }
        }
        obj2 = baseType;
    }
    

    在这个循环中,在"baseType=baseType.BaseType"之后,如果你再次检查FullName,现在是XElement... 但相反,它现在正在寻找 DataTemplateDataType={x:Type XElement}...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 2011-01-05
      • 2018-05-05
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多