1

How to bind xml to wpf treeview? I am using Prism mvvm pattern. I will prefer an IList for holding the data for looping.

I have tried http://geeklyeverafter.blogspot.com/2010/03/wpf-treeview-bound-to-xml-file.html and http://www.blogs.intuidev.com/post/2009/12/28/xml_to_treeview.aspx but nothing worked.

2
  • Are you using xml serialization? Commented Nov 8, 2013 at 22:43
  • @TraeMoore yes. I am using xml serialization Commented Nov 9, 2013 at 2:36

2 Answers 2

2

OK. It is The question is quite old now, but I think there is a simple way to bind XML to a TreeView. Maybe it is helpful for someone. XAML:

<Window.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="NodeTemplate">
            <Grid>
                <TextBlock Text="{Binding Path=Name}"/>
            </Grid>
        </HierarchicalDataTemplate>
</Window.Resources>

...

<TreeView x:Name="myTreeView" Grid.Column="0"
      ItemsSource="{Binding Path=Root.Elements}" 
      ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
      />

In the code behind I just create a XDocument (System.Xml.linq) and bind this one to the DataContext of the TreeView. For example like this:

private XDocument _theXML;
public XDocument TheXML {
    get => _theXML;
    set => _theXML = value;
}
public MainWindow()
{
    ...
    InitializeComponent();
    DataContext = this;
    TheXML = XDocument.Load(@"c:\file.xml");
    myTreeView.DataContext = TheXML;
    myTreeView.UpdateLayout();
}

That's it. The content of the XML file will be shown as a TreeView. If you like to see some more Details (Attributes, ...) you can refine the Template in the XAML code.

Sign up to request clarification or add additional context in comments.

Comments

0

The way I've done it is to create a method that builds the tree view to a treeview property. Set the WPF treeview items binding to the items property of the treeview property in your class. Of course implementing INotifyPropertyChanged in your ViewModelBase is essential.

I would be happy to give an example, but I do not have access to the internet on my PC at the moment

I did see in the post and do agree that this is not the most proficient way . However since you are already using xml serialization, the parsing of xml is done, now you just have to use the data.

I think that if you were not going to serialize, then the links you posted would hold more validity in the methodology you are you are trying to achieve. But that is just IMO. I will update with some working code when I get the chance tomorrow, the idea of data binding directly to xml sounds fun.

In the meantime check this link out. It looks pretty strait forward.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/cbdb2420-1403-436f-aa7f-b1e3b1acb398/binding-any-xml-document-to-wpf-treeview?forum=wpf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.