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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:40:26+00:00 2026-05-24T12:40:26+00:00

Say I have a 3-level data-bound WPF TreeView like this one: a aa aaa

  • 0

Say I have a 3-level data-bound WPF TreeView like this one:

  • a
    • aa
      • aaa
      • bbb
      • ccc
  • b
    • aa
    • bb
      • aaa
  • c
    • aa
      • aaa
    • bb
    • cc
      • aaa
      • bbb
      • ccc

where the selected node is bbb under cc under c. Is there a way to bind to say, cc, from some control living outside the TreeView?

UPDATE

Where I’m trying to get at here is to something similar to how you can bind to a selected item in a ListBox using this syntax:

<TextBox Text="{Binding Path=VM.Definitions/term}" />

where a ListBox ItemsSource is bound to VM.Definitions and ListBox.IsSynchronizedWithCurrentItem is set to True. I’m trying to figure out if there is a similar approach to bind to a specific level of a TreeView with scoped HierarchicalDataTemplates.

HOW I MADE IT WORK:

I accepted H.B. answers, but it was a combination of both his and Tim Murphy’s answers that made me “see the light”. Thing is that you can’t (AFAIK) bind to levels of a TreeView like you can bind to the single level of a ListBox (actually you can, but not to a specific level).

So I realized that all I have to do is to link back to my VM whatever is selected on each level of the TreeView whenever the selection changes. For example, say you have a TreeView with three levels Customer, Order, OrderItem. In the SelectedItemChanged, you set back in your VM each level.

If the selected item is a Customer, you then have VM.SelectedCustomer set to the Customer, and VM.SelectedOrder and VM.SelectedOrderItem set to null. If the selected item is an Order, then you set VM.SelectedCustomer to the parent item of the selected item, you set VM.SelectedOrder to the selected item, and VM.SelectedOrderItem to null. And so on.

A quick example (not my actual code, just to demo the concept):

void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if (treeViewLesson.SelectedItem == null) {
        VM.SelectedOrderItem = null;
        VM.SelectedOrder = null;
        VM.SelectedCustomer = null;
    }
    else if (treeViewLesson.SelectedItem is Customer) {
        VM.SelectedOrderItem = null;
        VM.SelectedOrder = null;
        VM.SelectedCustomer = treeViewLesson.SelectedItem as Customer;
    }
    else if (treeViewLesson.SelectedItem is Order) {
        VM.SelectedOrderItem = null;
        VM.SelectedOrder = treeViewLesson.SelectedItem as Order;
        VM.SelectedCustomer = VM.SelectedOrder.ParentCustomer;
    }
    else if (treeViewLesson.SelectedItem is OrderItem) {
        VM.SelectedOrderItem = treeViewLesson.SelectedItem as OrderItem;
        VM.SelectedOrder = VM.SelectedOrderItem.ParentOrder;
        VM.SelectedCustomer = VM.SelectedOrder.ParentCustomer;
    }
}
  • 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-24T12:40:28+00:00Added an answer on May 24, 2026 at 12:40 pm

    Edit: This kind of behavior is considerably complicated and the tools the TreeView gives you are not exactly thrilling. Sadly i cannot give you a complete answer on this but only a few pointers.

    • Your VM could expose a CurrentItem property which returns the item on the path of the selection at the current level, i.e. the item which owns the subbranch which ultimately contains the selected item.
    • Upward navigation would be quite helpful in this case i think, so having a Parent may be a good idea as well. e.g. if the selected item is changed you can go up the tree and change the CurrentItem property respectively, (the following may be rather bad/incorrect example code)

      private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
      {
          var oldItem = e.OldValue as TreeViewModel;
          // "Deselect" old branch
          var parent = oldItem.Parent;
          while (parent != null)
          {
              parent.CurrentItem = null;
              parent = parent.Parent;
          }
          // "Select" new branch
          var newItem = e.NewValue as TreeViewModel;
          parent = newItem.Parent;
          while (parent != null)
          {
              parent.CurrentItem = newItem;
              newItem = parent;
              parent = parent.Parent;
          }
      }
      
    • Bindings could then be done via a path of CurrentItems properties:

      {Binding Root.CurrentItem.CurrentItem.Value}
      

    Well, you can bind to pretty much anything, the question normally is how do you point to it.

    (Assume TreeView to be named tv, also assumes all items to be TreeViewItems rather than data)
    In this case if you can make a binding relative to the selection to get to that cc:

    {Binding SelectedItem.Parent, ElementName=tv}
    

    This of course will bind to cc with any subselection, not only bbb.

    Or you can drill down via indices, independent from any selection:

    {Binding Items[2].Items[2], ElementName=tv}
    

    You might want to be more specific about what you want to achieve.

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

Sidebar

Related Questions

Let's say we have a class that looks like this: class A { public:
Say I have data as follows: level,age 8,10 8,11 8,11 9,10 9,11 9,11 9,11
Say that have the following CTE that returns the level of some tree data
Say I have a class like this: @interface MyAwesomeClass : NSObject { @private NSString
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say I have a class named Frog, it looks like: public class Frog {
Let's say we have data as follows var data = { facets: [{ name
Let's say we have a solution with the following structure: Project.DAL - Data access

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.