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

  • Home
  • SEARCH
  • 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 7895861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:44:43+00:00 2026-06-03T07:44:43+00:00

I am trying to find a specific TreeViewItem whose Tag property is set to

  • 0

I am trying to find a specific TreeViewItem whose Tag property is set to a specific value. The below FindNode only works for the first level items or in other levels if the parent TreeViewItem is expanded. In the below example, if “FFF” is expanded, then FindNode works as expected. I am assuming that ContainerFromItem is returning null because the items have not been created. Is there a way to force the creation of all the TreeViewItems?

    <TreeView x:Name="__items">

        <TreeViewItem Header="AAA"
                      Tag="{x:Static my:Node.A}" />

        <TreeViewItem Header="BBB"
                      Tag="{x:Static my:Node.B}">

            <!-- Items will be added later. -->

        </TreeViewItem>

        <TreeViewItem Header="CCC"
                      Tag="{x:Static my:Node.C}" />

        <TreeViewItem Header="DDD"
                      Tag="{x:Static my:Node.D}" />

        <TreeViewItem Header="EEE"
                      Tag="{x:Static my:Node.E}" />

        <TreeViewItem Header="FFF"
                      Tag="{x:Static my:Node.F}">

            <TreeViewItem Header="GGG"
                          Tag="{x:Static my:Node.G}" />

            <TreeViewItem Header="HHH"
                          Tag="{x:Static my:Node.H}" />

        </TreeViewItem>

        <TreeViewItem Header="III"
                      Tag="{x:Static my:Node.I}" />

    </TreeView>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);    
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem a = FindNode(__items.ItemContainerGenerator, __items.Items, Node.H); 
    }

    private TreeViewItem FindNode(ItemContainerGenerator gen, ItemCollection items, Node value)
    {
        TreeViewItem oResult = null;

        foreach (var oItem in items)
        {
            TreeViewItem oTreeViewItem = (TreeViewItem)gen.ContainerFromItem(oItem);

            if (oTreeViewItem == null) { continue; }

            if ((Node)oTreeViewItem.Tag == value) { oResult = oTreeViewItem; break; }

            if (oTreeViewItem.Items.Count > 0)
            {
                oResult = FindNode(oTreeViewItem.ItemContainerGenerator, oTreeViewItem.Items, value);

                if (oResult != null) { break; }
            }
        }
        return oResult;
    }

}

public enum Node { A, B, C, D, E, F, G, H, I, J, }

Based on hbarck’s answer the correct FindNode implementation is:

    private TreeViewItem FindNode(ItemCollection items, Node value)
    {
        TreeViewItem oResult = null;

        foreach (var oItem in items)
        {
            TreeViewItem oTreeViewItem = (TreeViewItem)oItem;

            if ((Node)oTreeViewItem.Tag == value) { oResult = oTreeViewItem; break; }

            if (oTreeViewItem.Items.Count > 0)
            {
                oResult = FindNode(oTreeViewItem.Items, value);

                if (oResult != null) { break; }
            }
        }
        return oResult;
    }
  • 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-06-03T07:44:45+00:00Added an answer on June 3, 2026 at 7:44 am

    the easiest way to make sure that your TreeViewItems exist is probably to set IsExpanded=”True” on each of them, and to set IsVirtualizing to False on the TreeView. Just out of curiosity: what happens if you don’t use the ItemGenerator, but just iterate the Items collection directly instead? Since you’re not using a DataTemplate, I’d imagine that the Items collection should contain the hardcoded items from your XAML file.

    I’ve done the following test:

    <Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TreeView x:Name="TestTreeView">
            <TreeViewItem Header="Item 1">
                <TreeViewItem Header="Item 1 1"/>
                <TreeViewItem Header="Item 1 2"/>
            </TreeViewItem>
            <TreeViewItem Header="Item 2"/>
        </TreeView>
        <Button x:Name="TestButton" Click="TestButton_Click">Test</Button>
    </StackPanel>
    </Window>
    

    and code behind:

    Class MainWindow 
    
        Private Sub TestButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    
            Stop
            For Each t As TreeViewItem In Me.TestTreeView.Items
                Debug.Print("Item: {0}, Child count:{1}", t.Header, t.Items.Count)
            Next
        End Sub
    End Class
    

    The output in the debug window is

    Item: Item 1, Child count:2
    Item: Item 2, Child count:0
    

    which means that the items are instantiated and can be iterated, even in the child levels. Probably what gets into your way is the ItemContainerGenerator, which is actually only needed when you are using DataTemplates. Also, you probably have to wait until after the Loaded event of the window in order to have all items instantiated.

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

Sidebar

Related Questions

I am trying to find the fasted way to set a specific property of
I'm trying to find a struct I created earlier that has a specific value.
I'm trying to find a simple solution to a specific problem, which is a
I'm trying to find out if a date is between two specific dates or
I'm trying to find, then update, a specific DataRow in a DataTable. I've tried
Am trying to find some text only if it contains english letters and numbers
I'm trying to find a specific pattern on a string coming from MySQL table.
This is the preg_match i am trying to use to find specific text in
I am trying to find words starts with a specific character like: Lorem ipsum
I have been trying to find ways to save Datetime specific to Arabian Standard

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.