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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:53:05+00:00 2026-05-26T20:53:05+00:00

My TreeView is bound to an ObservableCollection and uses HierarchicalDataTemplates. I get the models

  • 0

My TreeView is bound to an ObservableCollection and uses HierarchicalDataTemplates.

I get the models from a web service.

Only when users click a node in tree, a web service call will be sent to get its child items.

My App has a TabControl, TreeView is on one tabpage, the other tabpage has a datagrid – it has some data selected from treeview. When an item in datagrid is right clicked, I want to locate the item on treeview. But now the issue is when I iterate the treeview,
say, I have an item called

A.A1.A11

and my TreeView has 3 items in the first level:

A
B
C

when I locate A.A1.A11, I want to expand A, A1, and highlight A11.

When I iterate the treeview, first I find A, it matches first path of A.A1.A11,
so I send a web service request to get A’s children.

Once I get that, DataContext of the TreeViewItem of A is updated, but the TreeViewItem itself is not.

So when I check A.Items, it is empty and iteratation is unable to continue.

How can I refresh the TreeView & TreeViewItem when its ItemsSource changes?

Here is the xaml for treeview definition

<TreeView x:Name="TreeRoot" 
          ItemsSource="{Binding RootItems}" 
          TreeViewItem.Expanded="TreeViewExpanded"
          TreeView.SelectedItemChanged="TreeRootSelectedItemChanged"
          ContextMenu="{StaticResource DataGridContextMenu}"
          PreviewKeyDown="TreeViewKeyDown"
          PreviewMouseLeftButtonDown="TreeViewPreviewLeftMouseButtonDown"
          PreviewMouseMove="TreeViewPreviewMouseMove" >
    <TreeView.Resources>
        <HierarchicalDataTemplate 
            DataType="{x:Type m:TreeModelBase}" 
            ItemsSource="{Binding Children}">    
            <StackPanel>
                <Image Source="{Binding ImageFilePath}" />
                <TextBlock Text="{Binding Name}" />
                <!-- other items removed for brevity -->
                <StackPanel.Style>
                <Style TargetType="{x:Type StackPanel}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsSelected}"
                                     Value="True">  
                        <Setter Property="Background" Value="DodgerBlue"/>
                        <Setter Property="TextBlock.Foreground" 
                                Value="White"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsSelected}"
                                     Value="False">  
                        <Setter Property="Background" Value="White"/>
                        <Setter Property="TextBlock.Foreground" 
                                Value="Black"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
  • 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-26T20:53:06+00:00Added an answer on May 26, 2026 at 8:53 pm

    I found & managed to get it work. but it is really not optimal at all. What I did, iterate viewModel first, send web service request to get A’s children, then send another request to get A1’s children. This is the first pass. Second, search treeview from root and generate ItemContainers while iteration. First, search root of tree, get node A (treeviewItem), then apply template, get container from ItemContainerGenerator (see detail @ How to refresh treeview when ItemsSource changes?), then I am able to find the item on treeview and expand its path. Anyone knows a better way, let me know. thanks

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

Sidebar

Related Questions

I have bound a WPF TreeView to an ObservableCollection. When a Connection node gets
I have a TreeView that is bound to an ObservableCollection in my ViewModel. I
I have a TreeView bound to an XmlDataProvider . The name of a node
In Silverlight 3 I have a TreeView bound to an ObservableCollection in my ViewModel.
Scenario I have a TreeView that is bound to ObservableCollection<T> . The collection gets
I'm using a WPF TreeView control, which I've bound to a simple tree structure
I'm using the TreeView component from the Silverlight toolkit and I'm trying to get
I have a TreeView that is bound to a tree of ViewModel instances. The
In my WPF application, I have a treeview. This treeview is bound to a
I have bound a TreeView to an XMLDataProvider. The TreeView displays the data as

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.