Click here to Skip to main content
16,001,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Create a WPF application to visualize a arbitrary XML document in a treeview and edit the treeview items
Posted

1 solution

<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>

<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}">




<xmldataprovider x:key="xmlDP" xpath="*">
<x:xdata>
<inventory xmlns="">
<books>
<book isbn="0-7356-0562-9" stock="in" number="9">
<title>XML in Action
XML Web Technology

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

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

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

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


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

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







<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"/>



 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900