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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:31:54+00:00 2026-05-25T22:31:54+00:00

In my application I build a TreeView by a binding. The ItemSource contains a

  • 0

In my application I build a TreeView by a binding. The ItemSource contains a list of items of type X.
X has a property ‘Properties’ some of these properties are references to an item in the parent list.

The TreeView starts at a selected root (an item from the list). All properties which are references are added to this root as TreeViewItems. However, they can be expanded too until the end is reached, or not; I mean that this could theoretically be an infinite recursion for it is possible that a reference refers to itself.

The question now is, how do I know which Item is selected?
I need to know the level and all its parents.

I thought up these things:

  • While expanding the tree, keep track of the levels and parents.
    This does not work obviously because after selecting an other item below a different parent, I lose track of the other path.
  • Check for the e.OldValue. This also doesn’t work for the same reason of the previous try.
  • While expanding (and with that creating) the TreeView, construct a TreeView like variable in c# behind the code.
    At first this seems to be quite close to the solution since you cannot remove nodes from the TreeView ones they are created. For example: You go to the 5th level and collapsed the first level. If you expand the first level you’ll see the the path exacly the same as before the collapse.

To create the TreeView in this way I had to override the DataTemplateSelector. (C#)

public class TreeViewDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;
        if (element != null && item != null)
        {
            if (item is Entity)
                return element.FindResource("XTemplate") as DataTemplate;
            else
                return element.FindResource("PropertyWithReferenceTemplate") as DataTemplate;
        }
        return null;
    }
}

The Binding and HierarachialDataTemplate. (Xaml)

<TreeView ItemsSource="{Binding ElementName=view, Path=XDefinition}" ItemTemplateSelector="{StaticResource treeViewDataTemplateSelector}"  SelectedItemChanged="TreeView_SelectedItemChanged">
    <TreeView.Resources>
        <HierarchicalDataTemplate x:Key="XTemplate" ItemsSource="{Binding Path=Properties}">
            <TextBlock Text="{Binding Path=Name}" />                            
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="PropertyWithReferenceTemplate" ItemsSource="{Binding Path=ReferenceToX.Properties,}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

Can anybody help me on this problem?

If there is anything else you need to know please tell me and I’ll update my question.

Solution
I had to change my event SelectedItemChanged=”TreeView_SelectedItemChanged” to TreeViewItem.Selected=”TreeViewItem_Selected”. With the help of the LogicalTreeHelper.GetParent i was able to retrieve the correct parent.

  • 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-25T22:31:55+00:00Added an answer on May 25, 2026 at 10:31 pm

    What about looking up the VisualTree? You can go up until you reach the top item. Before you can evaluate all single items.

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

Sidebar

Related Questions

I have a WorldWind application build based on the Java SDK. It has a
We have an WPF Application build on .net framework 3.5. Some testers find if
I have a java application build upon Spring 3. This project has another jar
I am using ASP.NET MVC to build my application. I have a treeview (with
I have a treeview control in my web application. I build this tree view
I have a flex application build with actionscript 3 on flex builder with unit
For an application build on Spring MVC + Groovy + Google App Engine i
I have an application build in C# for reading and saving SMS and MMS,
I have a simple ticket logging application build on LAMP. I am currently playing
Currently we are running an application build in VB6,ASP,Commerce Server2000 and Sql Server 2000.

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.