I have spend a lot of time trying to figure out how to bind data in my XML file to the TreeView control but I do not know where to start. I even tried going through Two-way binding of Xml data to the WPF TreeView and Josh Smith's code sample on codeproject, but still can't understand how to begin!!!
I have XML in in a file "C:\SPDependencies.xml" (I can change the format if required)!!!:
<node type="SPDependencies" Name="SPDependencies">
<node type="StoredProc" Name="SP1">
<node type="OperationType" Name="Type1">
<node type="TableName" Name="Table1"/>
<node type="TableName" Name="Table2"/>
</node>
<node type="OperationType" Name="Type2">
<node type="TableName" Name="Table1"/>
<node type="TableName" Name="Table2"/>
</node>
.....
</node>
<node type="StoredProc" Name="SP2">
<node type="OperationType" Name="Type1">
...
...
</node>
</node>
I need to display this in the Treeview control in the following format:
<SP1>
<Type1>
<Table1>
<Table2>
<Table3>
<Type2>
<Table1>
<Table2>
<Table3>
<SP2>
<Type1>
........
Thanks, Abhi.
Given the following xml file:
<node type="SPDependencies" Name="SPDependencies">
<node type="StoredProc" Name="SP1">
<node type="OperationType" Name="Type1">
<node type="TableName" Name="Table1"/>
</node>
<node type="OperationType" Name="Type2">
<node type="TableName" Name="Table1"/>
</node>
</node>
<node type="StoredProc" Name="SP2">
<node type="OperationType" Name="Type1">
</node>
</node>
</node>
View:
<Window x:Class="Tree.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tree"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Window.Resources>
<HierarchicalDataTemplate x:Key="template">
<TextBlock Text="{Binding XPath=@Name}" />
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="node" />
</HierarchicalDataTemplate.ItemsSource>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid DataContext="{Binding Path=XmlData}">
<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource template}">
</TreeView>
</Grid>
</Window>
View model:
public class ViewModel
{
public XmlDataProvider XmlData { get; set; }
public ViewModel()
{
XmlData = new XmlDataProvider();
XmlData.Source = new Uri(@"C:\input.xml");
XmlData.XPath = "node";
}
}
Output:

If you only want to show the nodes below the root, simply change the XPath to:
XmlData.XPath = "/node/node";
Heres the tree:
<Window.Resources>
<HierarchicalDataTemplate DataType="node"
ItemsSource="{Binding XPath=node}">
<TextBox Width="Auto"
Text="{Binding XPath=@Name, UpdateSourceTrigger=PropertyChanged}" />
</HierarchicalDataTemplate>
<XmlDataProvider
x:Key="xmlDataProvider"
XPath="node" Source="C:\Data.XML">
</XmlDataProvider>
</Window.Resources>
<Grid>
<StackPanel>
<Button Click="Button_Click">Save</Button>
<TreeView
Width="Auto"
Height="Auto"
Name="treeview"
ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=.}"/>
</StackPanel>
</Grid>
I've added a simple button to save changes. So for your Button_Click method in code behind:
XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider;
dataProvider.Document.Save(dataProvider.Source.LocalPath);
See here for an article about data binding and WPF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With