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