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

  • Home
  • SEARCH
  • 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

Since I am trying to learn WPF I see more and more the use
Trying to learn a bit of CSS and I want a horizontal navbar and
In trying to learn how to create objects in ActionScript, I have had no
I'm trying to learn C. As a C# developer, my IDE is Visual Studio.
I am trying to learn the keyboard shortcuts in Visual Studio in order to
I'm trying to learn RegEx in Ruby, based on what I'm reading in The
I'm trying to learn C++ so forgive me if this question demonstrates a lack
I am trying to learn some of the basic and advanced features of visual
I'm trying to learn to use SDL for a little game I'm writing ,
I am trying to learn CodeIgniter to use for a shopping site, but I

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.