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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:05:32+00:00 2026-05-25T23:05:32+00:00

I want to use a hierarchical TreeView which I will populate programmatically. My XAML

  • 0

I want to use a hierarchical TreeView which I will populate programmatically.

My XAML file is as follows:

<Window.Resources>
    <HierarchicalDataTemplate 
        DataType="{x:Type local:MyTreeViewItem}" 
        ItemsSource="{Binding Path=Children}">
        <TextBlock Text="{Binding Path=Header}"/>
    </HierarchicalDataTemplate>
</Window.Resources>


<Grid>
    <TreeView Margin="12,12,422,33" Name="treeView1" SelectedItemChanged="treeView1_SelectedItemChanged" MouseDoubleClick="treeView1_MouseDoubleClick">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
</Grid>

The problem I’m having is that it seems that the bindings are applied only once the item is visible.

Suppose I populate the TreeView as follows:

private ObservableCollection<MyTreeViewItem> m_items;
private MyTreeViewItem m_item1;
private MyTreeViewItem m_item2;

public MainWindow()
{
    InitializeComponent();
    m_items = new ObservableCollection<MyTreeViewItem>();
    m_item1 = new MyTreeViewItem(null) {Header = "Item1"};
    m_item2 = new MyTreeViewItem(null) {Header = "Item2"};
    m_item1.Children.Add(m_item2);
    m_items.Add(m_item1);
    treeView1.ItemsSource = m_items;
}

I also have a button that selects m_item2:

private void button2_Click(object sender, RoutedEventArgs e)
{
    m_item2.IsSelected = true;
    m_item2.IsExpanded = true;
}

Now, if I launch the program and the TreeView only shows Item1 (Item2 is hidden because Item1 is not expanded), then clicking the button won’t select m_item2. If I expand Item1 (thus making Item2 visible), the button will select m_item2.

Examining the PropertyChanged event on m_item2, I see that it is set to null initially, and a delegate is registered only once it is visible.

This is a problem for me because I want to be able to programmatically select an item, even if its parent has not yet been expanded (e.g. I want to be able to find a node in the tree).

I suppose I can programmatically expand and collapse all nodes, but it seems there should be a better way. Can someone suggest a solution?

  • 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-25T23:05:33+00:00Added an answer on May 25, 2026 at 11:05 pm

    Two suggestions:

    1) Expand m_item1 before selecting m_item2

    2) Hold your Selections in a ViewModel (using MVVM-Pattern)

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

Sidebar

Related Questions

I have a transaction log file in CSV format that I want use to
I want to use the MultipleLookupField control in a web page that will run
What I want to do is simply use the eclipse package explorer in hierarchical
I want to write a rename function to replace String names (which represent hierarchical
I have the typical emp table from which i want to fetch the hierarchical
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components

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.