Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So I have asked a question on how to bind any XML document regardless of schema to tree view in the following way: 1. Bind XML Document to WPF TreeView via XML Provider and HierarchicalDataTemplate. 2. Display all nodes of the XML Document including those that have child nodes in following format:</p> <p>&gt;Node1 </p> <p>Node1 Contents</p> <pre><code> &gt;ChildNode1 ChildNode1 Contents &gt;ChildNode1'sChildNode ChildNode1'sChildNode Contents </code></pre> <p>&gt;Node2</p> <pre><code> Node2 Contents </code></pre> <p>Problem was that my TreeView was binding each XmlNode name property to TreeItem. In case of text XmlNode it would bind #text to the TreeItem which was not what I wanted.</p> <p>So via a post on MSDN forum I got my answer: <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cbdb2420-1403-436f-aa7f-b1e3b1acb398/" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cbdb2420-1403-436f-aa7f-b1e3b1acb398/</a></p> <p>So the trick was to use triggers to set value based on type of node encountered.</p> <p>Caveat is that other types of nodes will be ignored and XML document may contain diffrent elements, so this might not work for every type of node encountered.</p> <p>Here's XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="Window1" Height="250" Width="450"&gt; &lt;Window.Resources&gt; &lt;HierarchicalDataTemplate x:Key="NodeTemplate"&gt; &lt;TextBlock x:Name="text" Text="?" /&gt; &lt;HierarchicalDataTemplate.ItemsSource&gt; &lt;Binding XPath="child::node()" /&gt; &lt;/HierarchicalDataTemplate.ItemsSource&gt; &lt;HierarchicalDataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=NodeType}" Value="Text"&gt; &lt;Setter TargetName="text" Property="Text" Value="{Binding Path=Value}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding Path=NodeType}" Value="Element"&gt; &lt;Setter TargetName="text" Property="Text" Value="{Binding Path=Name}"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/HierarchicalDataTemplate.Triggers&gt; &lt;/HierarchicalDataTemplate&gt; &lt;XmlDataProvider x:Key="xmlDataProvider"&gt;&lt;/XmlDataProvider&gt; &lt;/Window.Resources&gt; &lt;Grid &gt; &lt;TreeView Name="treeView1" Background="AliceBlue" ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}" ItemTemplate= "{StaticResource NodeTemplate}"/&gt; &lt;/Grid&gt; </code></pre> <p></p> <pre><code>public Window1() { InitializeComponent(); XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider; XmlDocument doc = new XmlDocument(); // Testdocument doc.LoadXml( @"&lt;root&gt; &lt;child1&gt;text1&lt;child11&gt;text11&lt;/child11&gt; &lt;/child1&gt; &lt;child2&gt;text2&lt;child21&gt;text21&lt;/child21&gt; &lt;child22&gt;text22&lt;/child22&gt; &lt;/child2&gt; &lt;/root&gt;"); dataProvider.Document = doc; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload