|
-
Jan 27th, 2010, 12:54 PM
#1
Thread Starter
Member
[WPF & VB.NET in VS2008] Can't populate Treeview with XML
I've read about 9 different topics on here regarding C# and VB.Net, as well as MSDN forums and google searches, and for the live of me I can't figure out why I can't get an XML file to correctly populate a Treeview. I've tried the examples in threads such as the following:
Treeview control
[RESOLVED] Import XML file directly into Treeview
[RESOLVED] TreeView Control
but none work for me, so hoping someone can help.
I'm trying to make somewhat of a Document Viewer for our internal support people, using a simple WPF Window, A treeview Control and A DocumentViewer control. Originally I was going to code the treeview manually in the XAML content, but other teams have expressed an interest in using the framework of the viewer and generating their own content, so I have to switch away from hard coding values in the application, and move to an external file, namely XML.
What I've got so far is this:
TreeItems.xml (My XML list of items for the TreeView):
Code:
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<Item ItemName="SR-650-101">
<Topic TopicName="NoPower" file="NoPower.xps" />
<Topic TopicName="SN12345" file="SN12345.xps" />
</Item>
<Item ItemName="SR-650-103">
<Topic TopicName="cantdiscover" file="cantdiscover.xps" />
<Topic TopicName="cantreset" file="noreset.xps" />
</Item>
</Items>
In my Window.xaml code, I have the following to define the XML resource:
Code:
<window.Resources>
<XmlDataProvider x:Key="TreeItems" XPath="Items/Item" Source="C:\TreeItems.xml"/>
</window.Resources>
And in the XAML for the treeview definition, I have the following:
Code:
<TreeView Grid.Column="1" Margin="47,139,45,203" Name="TreeView1" ItemsSource="{Binding Source={StaticResource TreeItems}, XPath=@ItemName}" />
The code above only populates my Treeview with the first item, "SR-650-101", it doesn't "walk" the file.
This is my first time ever working with XML files before, and it's proving to be quite challenging. What my goal for the XML file is this: I'm trying to get it so that the treeview gets populated with the contents of the XML file correctly, and then when someone clicks on one of the "topic items" the "file" gets loaded into the document viewer. I've got it somewhat working by storing the filename in the TAG field of the XAML, and then using the following to load:
Code:
Private Sub TreeView1_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles TreeView1.SelectedItemChanged
Dim item As TreeViewItem = e.NewValue 'Get Selected Tree Item
If item.Tag <> "" Then 'If Item Has a Tag assigned, use the Tag to open the file.
Dim xps As New XpsDocument(DocPath + item.Tag, System.IO.FileAccess.Read)
DocumentViewer1.Document = xps.GetFixedDocumentSequence()
End If
End Sub
Normally I try to figure out this stuff on my own so that I can learn from it, but it's been 3 days now I'm stuck on trying to get XML to load and work properly and it's driving me nuts, so hoping someone can help with some code or something that pertains to VB.net, WPF and VS 2008.
Thanks in advance
Edit: btw, the look i am trying to achieve is shown below, which is how I have it set up with values hardcoded into the xaml content.
Last edited by evanrich; Jan 27th, 2010 at 02:00 PM.
-
Jan 27th, 2010, 05:08 PM
#2
Re: [WPF & VB.NET in VS2008] Can't populate Treeview with XML
All of those threads you have been looking at are not for WPF though... Have you googled for something like "wpf treeview xml" ? This is one of the first results if you do and looks like it has a working code example in: http://social.msdn.microsoft.com/For...-a2f281b8dc3e/ and this which is more of a general tutorial on binding to XML, not specific to the TreeView control: http://joshsmithonwpf.wordpress.com/...inding-to-xml/
Hope that helps
-
Feb 2nd, 2010, 05:11 PM
#3
Thread Starter
Member
Re: [WPF & VB.NET in VS2008] Can't populate Treeview with XML
Chris,
Thanks for the reply. I had already seen those links, however I tried some of the code listed in one of the links on those links, and got somewhere. I can get the treeview to populate correctly, but now I have another question..
I got my source from this link:
http://social.msdn.microsoft.com/for...-4939fcd6f04e/
A section of the XML looks as follows:
Code:
<type>
<name>2024-0 Bare</name>
<cost>8.55</cost>
<unit>LB</unit>
<density>0.320</density>
</type>
the name gets populated in the treeview, which is great, but how would I go about taking another value, say the cost, and using that to load the document into the viewer? to explain better lets say i changed the above code to read:
Code:
<type>
<name>2024-0 Bare</name>
<document>BareMetal.XPS</document>
</type>
how can I get the code to pass the <document> value to the document viewer when a user clicks that item in the list?
50% of the way there 
Thanks.
edit that sounds a little confusing, I guess what I'm trying to do is have it so that on a "selectedItemChanged" event, whatever Item in the treeview is selected, it's corresponding "<document>" text gets fed as a variable to the document viewer"
grrr.
Last edited by evanrich; Feb 2nd, 2010 at 07:10 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|