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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:39:10+00:00 2026-05-13T07:39:10+00:00

I have a TreeView whose ItemsSource is set to a Model I have. 3

  • 0

I have a TreeView whose ItemsSource is set to a Model I have. 3 levels deep is an object whose state can change and, rather than write a View and ViewModel for each stage (very lengthy business) I wanted to just update this using code.

So basically I have an event that is fired once my model updates, I catch it then find the TreeViewItem associated with this bit of data. My issue now is I have NO idea on how to update the binding on it to reflect the new value!

Can anyone help?

I know this isn’t best practice but I really don’t want to have to waste time writing a massive amount of code to just update one thing.

Thanks
Chris

  • 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-13T07:39:11+00:00Added an answer on May 13, 2026 at 7:39 am

    This example works, though it’s only two (not 3) levels deep. It shows a simple 2-level hierarchical treeview with parent items A, B, and C, with numbered children (A.1, B.1, etc). When the Rename B.1 button is clicked, it renames B.1 to “Sylvia”.

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace UpdateVanillaBindingValue
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            private DataClass _data;
    
            public Window1()
            {
                InitializeComponent();
                var data = CreateData();
                DataContext = _data = data;
            }
    
            private DataClass CreateData()
            {
                return new DataClass
                   {
                       Parents=new List<Parent>
                           {
                               new Parent{Name="A",Children=new List<Child>{new Child{Name="A.0"},new Child{Name="A.1"}}},
                               new Parent{Name="B",Children=new List<Child>{new Child{Name="B.0"},new Child{Name="B.1"},new Child{Name="B.2"}}},
                               new Parent{Name="C",Children=new List<Child>{new Child{Name="C.0"},new Child{Name="C.1"}}}
                           }
                   };
            }
    
            private void Rename_Click(object sender, RoutedEventArgs e)
            {
                var parentB = _data.Parents[1];
                var parentBItem = TheTree.ItemContainerGenerator.ContainerFromItem(parentB) as TreeViewItem;
                parentB.Children[1].Name = "Sylvia";
    
                var parentBItemsSource = parentBItem.ItemsSource;
                parentBItem.ItemsSource = null;
                parentBItem.ItemsSource = parentBItemsSource;
            }
        }
    
        public class DataClass
        {
            public List<Parent> Parents { get; set; }
        }
    
        public class Parent
        {
            public string Name { get; set; }
            public List<Child> Children { get; set; }
        }
    
        public class Child
        {
            public string Name { get; set; }
        }
    }
    
    <Window x:Class="UpdateVanillaBindingValue.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Grid.Resources>
                <DataTemplate x:Key="ChildTemplate">
                    <TextBlock Margin="50,0,0,0" Text="{Binding Name}" />
                </DataTemplate>
                <HierarchicalDataTemplate x:Key="ParentTemplate" ItemsSource="{Binding Children}" ItemTemplate="{StaticResource ChildTemplate}">
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </Grid.Resources>
            <TreeView x:Name="TheTree" ItemsSource="{Binding Parents}" ItemTemplate="{StaticResource ParentTemplate}" />
            <Button VerticalAlignment="Bottom" HorizontalAlignment="Center" Content="Rename B.1" Click="Rename_Click" />
        </Grid>
    </Window>
    

    This is a hack, but it re-evaluates the DataTemplate every time it’s ItemsSource property changes.

    Ideally, you would implement INotifyPropertyChanged on your model object class that this TreeViewItem is bound to, and have it fire the PropertyChanged event when that value changes. In fact, you should be careful that you aren’t incurring a memory leak because it doesn’t: Finding memory-leaks in WPF Applications.

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

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • 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 b = a(:,[1,end]) This means: all rows (:), first and… May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Have you built the application? May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer As jAndy states the problem is event bubbling here, but… May 16, 2026 at 7:03 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.