Click here to Skip to main content
12,626,216 members (33,417 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF C#4.0
How to Create a WPF application to visualize a arbitrary XML document in a treeview and edit the treeview items
Posted 21-Sep-12 2:16am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<window x:class="CS.XML_To_TreeView" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="How to load XML into a TreeView control"
Height="400"
Width="550">
<window.resources>
<!-- This HierarchicalDataTemplate will visualize all XML-nodes -->
<hierarchicaldatatemplate x:key="NodeTemplate">
<textblock x:name="tb">
<hierarchicaldatatemplate.itemssource>
<binding xpath="child::node()">

<hierarchicaldatatemplate.triggers>
<datatrigger binding="{Binding Path=NodeType}" value="Text">
<setter targetname="tb" property="Text" value="{Binding Path=Value}">

<datatrigger binding="{Binding Path=NodeType}" value="Element">
<setter targetname="tb" property="Text" value="{Binding Path=Name}">



<!-- This will contain the initial XML-data as well as any other file the user may pick at runtime. -->
<xmldataprovider x:key="xmlDP" xpath="*">
<x:xdata>
<inventory xmlns="">
<books>
<book isbn="0-7356-0562-9" stock="in" number="9">
<title>XML in Action
<summary>XML Web Technology

<book isbn="0-7356-1370-2" stock="in" number="8">
<title>Programming Microsoft Windows With C#
<summary>C# Programming using the .NET Framework

<book isbn="0-7356-1288-9" stock="out" number="7">
<title>Inside C#
<summary>C# Language Programming

<book isbn="0-7356-1377-X" stock="in" number="5">
<title>Introducing Microsoft .NET
<summary>Overview of .NET Technology

<book isbn="0-7356-1448-2" stock="out" number="4">
<title>Microsoft C# Language Specifications
<summary>The C# language definition


<cds>
<cd stock="in" number="3">
<title>Classical Collection
<summary>Classical Music

<cd stock="out" number="9">
<title>Jazz Collection
<summary>Jazz Music





<!-- The following two styles allow for easy expanding/collapsing of all nodes. -->

<style.resources>
<style targettype="TreeViewItem">
<setter property="IsExpanded" value="True" />




<style.resources>
<style targettype="TreeViewItem">
<setter property="IsExpanded" value="False" />




<grid>
<grid.rowdefinitions>
<rowdefinition height="Auto">
<rowdefinition height="*">
<rowdefinition height="Auto">


Content="Pick the XML document to be shown ..."
Margin="5"
Click="cmdLoadXml_Click"
ToolTip="Click here to pick an XML-document to be loaded."
/>
<treeview name="tv">
Background="LightYellow"
ItemTemplate= "{StaticResource NodeTemplate}"
ItemsSource="{Binding Source={StaticResource xmlDP}}"
Margin="5"
Grid.Row="1"
/>
<grid grid.row="2">
<grid.columndefinitions>
<columndefinition width="Auto">
<columndefinition width="Auto">
<columndefinition width="Auto">
<columndefinition width="*">
<columndefinition width="Auto">


Margin="5"/>
<textbox x:name="txt">
Margin="0,5"
Width="150"
Grid.Column="1"/>
<uniformgrid columns="2">
Margin="5,0"
Grid.Column="2">

Click="cmdChangeDisplayRootNode_Click"
Margin="3"
Padding="3"
ToolTip="Click here to use the path specified in the TB to the left as the root display node."
Content="_Go"/>

Click="cmdReset_Click"
Margin="3"
Padding="3"
ToolTip="Click here to reset the root display node (show all)."
Content="_Reset"/>

<uniformgrid columns="2">
Margin="5,0"
Grid.Column="4">

Content="Expand all"
Margin="3"
Padding="3"
ToolTip="Click here to expand all TreeView nodes"
Click="cmdExpandAll_Click"/>

Content="Collapse all"
Margin="3"
Padding="3"
ToolTip="Click here to collapse the TreeView"
Click="cmdCollapse_Click"/>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161205.3 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100