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 394087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:19:17+00:00 2026-05-12T16:19:17+00:00

I am trying to learn how to bind the following simple XML file to

  • 0

I am trying to learn how to bind the following simple XML file to a WPF TreeView:

<?xml version="1.0" encoding="utf-8" ?>
<Profiles>

  <Customer>
    <Name>Customer1</Name>    
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText1</DisplayText>
    </Profile>
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText2</DisplayText>
    </Profile>
  </Customer>

  <Customer>
    <Name>Customer2</Name>    
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText3</DisplayText>
    </Profile>    
  </Customer>

</Profiles>

Here is my XAML code of the attempt:

 <TreeView DockPanel.Dock="Left" Height="auto" Name="treeView1" Width="217"
           SelectedItemChanged="UIProfileTreeViewSelectedItemChanged"
           ItemsSource="{Binding}">
     <TreeView.DataContext>
         <XmlDataProvider Source="Profiles.xml" XPath="/Profiles/Customer"/>
     </TreeView.DataContext>
     <TreeView.Resources>
         <DataTemplate DataType="Customer">
             <TreeViewItem Header="{Binding XPath=Name}"
                           ItemsSource="{Binding XPath=Profile}"/>
         </DataTemplate>                
         <DataTemplate DataType="Profile">
             <TreeViewItem Header="{Binding XPath=DisplayText}" />
         </DataTemplate>                
     </TreeView.Resources> 
 </TreeView>

However the results prevent me from selecting individual profiles under a customer in the TreeView, if I click one profile the whole group gets highlighted like so:

alt text http://img38.imageshack.us/img38/4484/sberr.png

I clearly have a conceptual error with how databound items work. Any pointers?

  • 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. Editorial Team
    Editorial Team
    2026-05-12T16:19:17+00:00Added an answer on May 12, 2026 at 4:19 pm

    I was able to get this to work with the xaml below.

    There were two basic issues I ran into with your code:

    1. Both levels of your treeview were using “DataTemplate” instead of the “outer” one being defined as a “HierarchicalDataTemplate” — so you had to put the ItemsSource on the TreeViewItem, which appears to have caused the “selection as a group” issue
    2. I was having trouble getting selection to work at all using TreeViewItems, but after converting the DataTemplates to show a Label (you could display any sort of simple or complex set of controls in here), it worked perfectly. WPF automatically inserts “TreeViewItem” controls to wrap whatever you bind into a TreeView, so you don’t need to do it explicitly.

          <TreeView DockPanel.Dock="Left" Height="auto" Name="treeView1" Width="217" SelectedItemChanged="UIProfileTreeViewSelectedItemChanged" ItemsSource="{Binding}">
            <TreeView.DataContext>
              <XmlDataProvider Source="Profiles.xml" XPath="/Profiles/Customer"/>
            </TreeView.DataContext>
            <TreeView.Resources>
              <HierarchicalDataTemplate DataType="Customer" ItemsSource="{Binding XPath=Profile}">
                  <Label Content="{Binding XPath=Name}" />
              </HierarchicalDataTemplate>
              <DataTemplate DataType="Profile">
                  <Label Content="{Binding XPath=DisplayText}" />
              </DataTemplate>
            </TreeView.Resources>
          </TreeView>
      

    Hope this helps!

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

Sidebar

Related Questions

I was trying learn lex and yacc using the oreilly book. I tried following
Since I am trying to learn WPF I see more and more the use
I am just started learning WPF, and trying to learn the correct way to
I am an experienced coder who is trying to learn C# and WPF. Now,
Right now I'm trying to put together something really simple, learn from it, and
I am trying to learn backbone.js through the following example. Then I got stuck
Trying to learn asio, and I'm following the examples from the website. Why is
I'm trying to learn using Caliburn.Micro with WPF. How can I add multiple views
While trying to learn Play Framework I ran across the following problem: How do
I am trying to learn the MVVM pattern in WPF and I am running

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.