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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:11:05+00:00 2026-05-25T18:11:05+00:00

I have a TreeView implemented in WPF that is bound to some XML data

  • 0

I have a TreeView implemented in WPF that is bound to some XML data via the XElement class. When loading the XML file the first time the binding works fine. All the data populates the tree as expected. The problem occurs when I add and remove elements because nothing happens in the TreeView. Now I’ve done this before’ and I believe I remember not having to do any extra work for this work correctly. At least for the simple case of adding and removing items from the tree. I remember being surprised that this worked without any extra coding effort. I don’t have access to that code anymore so I can’t just look at what I already did. So I’m kind of stumped as to why I can’t get this to work now.

My WPF code is as follows.

<Window.Resources>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="ViewEditTreeTemplate">
        <StackPanel Orientation="Horizontal" Margin="2">
            <Label x:Name="ElementHeaderLabel" Padding="1" VerticalContentAlignment="Center" FontSize="16" Content="{Binding Path=DisplayName}" />
        </StackPanel>
    </HierarchicalDataTemplate>
</Window.Resources>


<Grid>
    <TreeView  Name="DataTree" ItemTemplate ="{Binding Source={StaticResource ViewEditTreeTemplate}}" Margin="0,0,0,53" />
</Grid>

I’m attaching my XML document in the code behind as follows. Keep in mind that this appears to be working since the tree auto populates with the information from the XML data just fine.

XElement NewElement = new XElement(XElement.Load(FilePath));
List<XElement> TempList = new List<XElement>();
TempList.Add(NewElement);
DataTree.ItemsSource = TempList;

In the code behind when I go to add or remove elements I do as follows:

// When removing an element
Element.Remove();            //Element is of type XElement

// When adding an element
ParentElement.Add(NewElement);    //ParentElement and NewElement are of type XElement

I have this strong feeling that when I did this before, I actually didn’t have to do anything special. The .Remove() and .Add() routines somehow notified the bindings that the items in .Elements() have changed and the screen updated automatically. Whatever the case, its not working this time around. Does anyone know why?

  • 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-25T18:11:06+00:00Added an answer on May 25, 2026 at 6:11 pm

    Ok, I have a solution to my own problem. I’m not sure why I thought this would originally work without having to do anything special, but that seems to not be the case.

    To clarify the problem a little more, I was not using the the XElement object as is, I had it wrapped by another class so I could add a few more features to it. This was good though because it turns out I needed the INotifyPropertyChanged interface and to override a few methods in order to get the behavior I wanted. My actual class header looks like this.

    public class TreeElement : XElement, INotifyPropertyChanged
    {
       ...
    }
    

    Since my TreeView was binding on the XElement.Elements() routine, it was not receiving notifications when I added or removed elements. The reason why is because XElement.Elements() is NOT a dependency property. Its a routine. I knew this ahead of time but for some reason I was stubborn and still thought it was going to work. So what I needed to add was the INotifyPropertyChanged interface, and then invoke the NotifyPropertyChanged() routine whenever operations occurred that would cause the data from XElement.Elements() to be changed. Namely, they XElement.Add() and XElement.Remove() routine. There are several more but I’ll just talk about these two.

    These routines are not overridable, but they can be hidden using the “new” key word. Here is the new implementation for those routines.

    public new void Remove()
    {
        XElement parent = this.Parent;
        base.Remove();
    
        if ((parent != null) && (parent.GetType() == typeof(TreeElement)))
        {
        //need to tell parent that they are losing an element
        ((TreeElement)parent).NotifyPropertyChanged("Elements");
        }
    }
    
    
    public new void Add(object Content)
    {
        base.Add(Content);
        NotifyPropertyChanged("Elements");
    }
    

    As you can see, even though .Elements() is not a property the NotifyPropertyChanged(“Elements”) method works just fine at notifying my binding ‘ItemsSource=”{Binding Path=Elements}”‘. So now when I update in the code behind using .Add() or .Remove() my tree auto updates.

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

Sidebar

Related Questions

I have a treeview that is bound to a collection and each item in
I have a control that inherits from TreeView (System.Windows.Controls.TreeView from WPF Framework) and it
I have a TreeView that is bound to a tree of ViewModel instances. The
I have a treeview control in a Windows Forms project that has checkboxes turned
I have a TreeView control in my WinForms .NET application that has multiple levels
I have a treeview control on an aspx page. The data comes from database
I have a TreeView that launches a new window when each of its TreeViewItems
I have treeview and a context menu that is shown for every node. One
I have a TreeView that is generated in code, and will look like this
I'm working on my first C#/WPF project (I'm a Java/Web developer with some Flex/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.