からサブクラス化するXElement
と、要素名を として使用するDataTemplate
ために機能する は、サブクラスでは機能しません。何か案が?XElement
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> -->