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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:45:47+00:00 2026-05-13T06:45:47+00:00

I’m trying to select a TreeViewItem by ID, but having problems getting it to

  • 0

I’m trying to select a TreeViewItem by ID, but having problems getting it to work past the first (root) level. I’ve done so much reading round on this and am using the method below.

private static bool SetSelected(ItemsControl parent, INestable itemToSelect) {
    if(parent == null || itemToSelect == null) {
        return false;
    }
    foreach(INestable item in parent.Items) {
        if(item.ID == itemToSelect.ID) { // just comparing instances failed
            TreeViewItem container = parent.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
            if(container != null) {
                container.IsSelected = true;
                container.Focus();
                return true;
            }
        }
        ItemsControl childControl = parent.ItemContainerGenerator.ContainerFromItem(item) as ItemsControl;
        if(SetSelected(childControl, itemToSelect))
            return true;
    }
    return false;
}

INestable is the base level interface, implemented by IGroup and IAccount:

public interface INestable {
        string ID { get; set; }
    ...
}
public interface IAccount : INestable { 
    ...
}
public interface IGroup : INestable { 
    public IList<INestable> Children
    ...
}

I think it must have something to do with the datatemplates (perhaps):

<HierarchicalDataTemplate DataType="{x:Type loc:IGroup}" ItemsSource="{Binding Children}" x:Key="TreeViewGroupTemplate">
<HierarchicalDataTemplate DataType="{x:Type loc:IAccount}" x:Key="TreeViewAccountTemplate">

The Template selector for the treeview returns thr group template for IGroups and the account template for IAccounts:
<conv:TreeTemplateSelector x:Key="TreeTemplateSelector" AccountTemplate="{StaticResource TreeViewAccountTemplate}" GroupTemplate="{StaticResource TreeViewGroupTemplate}"/>
<TreeView ItemTemplateSelector="{StaticResource TreeTemplateSelector}">

It works for all top level items, just nothing below that, and debugging confirms parent.ItemContainerGenerator does contain the items for all levels.

I know there’s a lot of code but I’m burning through hours trying to get this to work. Thanks for any help. 🙂

  • 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-13T06:45:47+00:00Added an answer on May 13, 2026 at 6:45 am

    The problem is that the nested ItemContainerGenerators aren’t generated all at the beginning, they are generated on-demand. And even more so, they are generated in a separate thread, so you have to listen to a StatusChanged on the generator to be sure it is ready =(

    Some people suggest to play with the Dispatcher (like in this Bea’s post). I tried to implement the Dispatcher solution, but it didn’t work for some reason… the generators are still empty =(

    So I ended up with another one, where you specifically ask the tree to update its layout, which causes the generation for the expanded nodes. Here’s the final method… you might want to test it a little to verify that it suites your needs. It might collapse some nodes that were expanded before its run.

        private static bool SetSelected(TreeView treeView, ItemsControl parentControl, INestable itemToSelect)
        {
            if (parentControl == null || itemToSelect == null)
            {
                return false;
            }
            foreach (INestable item in parentControl.Items)
            {
                TreeViewItem container = parentControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
    
                if (item.ID == itemToSelect.ID)
                { // just comparing instances failed
                        container.IsSelected = true;
                        container.Focus();
                        return true;
                }
                container.IsExpanded = true;
                treeView.UpdateLayout();
                WaitForPriority(DispatcherPriority.Background);
                if (SetSelected(treeView, container, itemToSelect))
                    return true;
                else
                    container.IsExpanded = false;
            }
            return false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 387k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I personally in that situation would use a Fake Service.… May 15, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer You can easily build a dictionary style collection... function getQueryStrings()… May 15, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer There's no direct way to remove pages from a PDF… May 15, 2026 at 12:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.