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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:54:27+00:00 2026-05-20T01:54:27+00:00

I have a TreeView that the user navigates to select an item for display

  • 0

I have a TreeView that the user navigates to select an item for display in a grid. Briefly the XAML looks like this:

    <local:TreeViewEx x:Name="theTreeView" ItemsSource="{Binding theData}">

                        <local:TreeViewEx.ItemTemplate>
                            <sdk:HierarchicalDataTemplate ItemsSource="{Binding theChildData}">
                                <TextBlock Text="{Binding Name}"/>
                            </sdk:HierarchicalDataTemplate>
                        </local:TreeViewEx.ItemTemplate>
                    </local:TreeViewEx>

 <Grid DataContext="{Binding ElementName=theTreeView, Path=SelectedItem}">
                            <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding}" />
                    <TextBlock Text="{Binding Name}" /></StackPanel>
        </Grid>

As the user clicks through the treeview the viewmodel type name is displayed along with the value of the Name property. Perfect. Howerver the user can also execute a search of the treeview (following to Josh Smith) which sets the IsSelected property of the TreeViewItem. Once that happens the {Binding} displays TreeViewItemEx rather than the ViewModel type name, and of course the Name property is not displayed.

How is that possible that the selectedItem would sometimes by the ViewModel, and sometimes be the TreeViewItem?

  • 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-20T01:54:28+00:00Added an answer on May 20, 2026 at 1:54 am

    If you replace your grid with a ContentControl you can then use a DataTemplateSelector.

    <ContentControl Content="{Binding ElementName=theTreeView, Path=SelectedItem}" 
                    ContentTemplateSelector="{StaticResource TreeViewItemSelector}" />
    

    On the DataTemplateSelector you can then reference two templates for the different types

        <DataTemplate x:Key="ModelTemplate">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding}" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    
        <TreeViewItemSelector x:Key="TreeViewItemSelector"
                              ModelTemplate="{StaticResource ModelTemplate}"
                              TreeItemTemplate="{StaticResource TreeItemTemplate}" />
    

    In the selector you will then want logic like this

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is ModelType)
                return ModelTemplate;
            if (item is TreeViewItem)
                return TreeItemTemplate;
            throw new NotImplementedException();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TreeView that allows users to select certain elements of hierarchical data
I currently have a treeview that displays data using a hierarchicaldatatemplate and when the
I have a treeview menu where user can drag something and drop on a
I have an object for like this: var obj = { c: { games:
I have a tree view with checkboxes in c#, I want that when the
In my MVVM app I have a treeview representing records in a database. My
I have a class Category, that represents products category... So I created a View
I have the following setup: <asp:treeview cssclass=draggable><items...> <table /> And I set it up
I have two forms named 'mainForm' and 'addRslt'. The idea is when users click
I've been using non-OOP languages (Mostly scripting or web development) such as Lua for

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.