0

I have some xml:

<Test>
  <thing location="home" status="good"/>
  <thing location="work" status="bad"/>
  <thing location="mountains" status="good"/>
</Test>

The leaves on the TreeView are the values of the status attribute; the nodes will be the value of the location attribute.

├──bad
│.....└──work
└──good
.......├──home
.......└──mountains

Currently, I populate the TreeView (or TabControl) manually, iterating through the xml, adding the nodes to the appropriate leaf.
Can this be done via databinding? I'm guessing a Converter will be involved...
Thanks for any advice.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Number8
  • 12,322
  • 10
  • 44
  • 69

1 Answers1

0

Assuming you are going to bind to an XmlDataSource you could use a TransformFile with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/Test">
    <Test>
      <good>
        <xsl:for-each select="thing[@status='good']">
           <xsl:element name="{@location}"/>
        </xsl:for-each>
      </good>
      <bad>
        <xsl:for-each select="thing[@status='bad']">
          <xsl:element name="{@location}"/>
        </xsl:for-each>
      </bad>
    </Test>
  </xsl:template>
</xsl:stylesheet>

Add an XPath="/Test/*" property to the XmlDataSource to remove the "Test" root element.

Martin Owen
  • 5,221
  • 3
  • 35
  • 31
  • Thanks for the reply, that's very helpful. I suspect I need DataTemplates in the xaml for the TreeView and TabControl items? – Number8 Aug 18 '09 at 13:24
  • You may not need DataTemplates, but my knowledge of WPF is limited. Try DataBinding without and see how it looks. The ASP.NET TreeView works if you just DataBind the XmlDataSource. I stupidly assumed your question was about an ASP.NET TreeView (you need a "wpf" tag on your question.) – Martin Owen Aug 19 '09 at 15:28