views:

263

answers:

2

Hello,
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 Tab control) 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.

A: 

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
Thanks for the reply, that's very helpful.I suspect I need DataTemplates in the xaml for the TreeView and TabControl items?
Number8
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
A: 

I was looking for the same type of information and found this question on Stack Overflow very helpful. You simply need to specify the XmlDataProvider and some HierarchicalDataTemplate elements. Then some simple data bindings with XPath attributes pulls it all together. Check out the link above for more details.

Glenn