Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 60477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:10:05+00:00 2026-05-10T18:10:05+00:00

I have an XML that needs to be databound to a WPF TreeView .

  • 0

I have an XML that needs to be databound to a WPF TreeView. Here the XML can have different structure. The TreeView should be databound generic enough to load any permutation of hierarchy. However an XAttribute on the nodes (called Title) should be databound to the TreeViewItem’s header text and not the nodename.

XML to be bound:

<Wizard>   <Section Title='Home'>     <Loop Title='Income Loop'>       <Page Title='Employer Income'/>       <Page Title='Parttime Job Income'/>       <Page Title='Self employment Income'/>     </Loop>   </Section>   <Section Title='Deductions'>     <Loop Title='Deductions Loop'>       <Page Title='Travel spending'/>       <Page Title='Charity spending'/>       <Page Title='Dependents'/>     </Loop>   </Section> </Wizard> 

XAML:

<Window x:Class='Wpf.DataBinding.TreeViewer'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:local='clr-namespace:Wpf.DataBinding'     Title='TreeViewer' Height='300' Width='300'>     <Window.Resources>         <HierarchicalDataTemplate ItemsSource='{Binding Path=Elements}' x:Key='TVTemplate'>             <TreeViewItem Header='{Binding Path=Name}'/>         </HierarchicalDataTemplate>     </Window.Resources>     <StackPanel>         <TreeView x:Name='_treeView' Style='{StaticResource TVallExpanded}'                 ItemsSource='{Binding Path=Root.Elements}'                 ItemTemplate='{StaticResource TVTemplate}' />     </StackPanel> </Window> 

XAML’s codebehind that loads XML to XDocument and binds it to TreeView

public partial class TreeViewer : Window {     public TreeViewer()     {         InitializeComponent();         XDocument doc = XDocument.Parse(File.ReadAllText(@'C:\MyWizard.xml'));         _treeView.DataContext = doc;     } } 

So in the XAML markup we are binding Name to TreeViewItem’s header.

<TreeViewItem Header='{Binding Path=Name}'/> 

However, I want to bind it to Title attribute of Section, Loop and Page in the Xml above. I read that it’s not possible to use XPath while binding XDocument. But there has to be a way to bind the Title attribute to TreeViewItem’s Header text. I tried using @Title, .[@Title] etc. But none seemed to work.

This thread on MSDN Forums has a similar discussion.

Any pointers would be greatly helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:10:06+00:00Added an answer on May 10, 2026 at 6:10 pm

    Hurrah !!! I figured out how to bind XAttribute. It is not intuitive and it’s not easily imaginable. But here is how it can be done.

    <TreeViewItem Header='{Binding Path=Attribute[Title].Value}'/> 

    It is hard to imagine that Title can directly be used in square braces.

    More @ this MSDN link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Turns out you just need to get the Ruby source… May 11, 2026 at 3:57 pm
  • added an answer I think a ClickOnce deployment Executable is your closest bet… May 11, 2026 at 3:57 pm
  • added an answer Valgrind comes with some default error suppression, but that probably… May 11, 2026 at 3:57 pm

Related Questions

Lets say I have a custom data type that looks something like this: public
I have an XML schema that represents a product in a DB, and I
I am putting an XSL together than will create a NAnt build script using
I am developing an app that needs to send text messages, so I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.