Changing the itemsSource of a treeview makes it's children invisible, when they were already display

Posted by Marnix Kraus on Stack Overflow See other posts from Stack Overflow or by Marnix Kraus
Published on 2010-06-10T07:58:26Z Indexed on 2010/06/10 8:02 UTC
Read the original article Hit count: 306

Filed under:
|
|
|
|

I found some strange problem in WPF, using the itemsSource of a treeview. I hope I can make this specific problem clear for you.

First; a story.
There is a treeview. It has a list with treeviewitems as itemsSource. This list is called _roots. There is another list, called _leafs. For as in a treeview, the _roots contain the _leafs in some hierarchical way. For example:

<TreeviewItem Header="Jungle">
    <TreeviewItem>
        <SpecialTreeviewItem Header="Monkey"/>
        <SpecialTreeviewItem Header="Apple"/>
    </TreeviewItem>
</TreeviewItem>

Now I am trying to switch between these two lists as itemsSource. It seemed to work fine, but it doesn't:

When the Jungle-item is un-expanded, and I change the itemsSource to _leafs, and change it back again to _roots, everything works fine and all items can be expanded and showed.

But when the Jungle-item is expanded (and the special items are already visible) and I change it to the _leafs itemsSource, and then change the itemsSource back to _roots, all special items have disappeared!!

Also, when I do the same as case 2, but first un-expand the Jungle-item again, the special items also disappear.

I did a lot of debugging, before posting this question here and come to the following conclusion:

  • Printing on the event: visibility changed, the visibility is set to false for all items that were already visible (that is, when _roots become visible, the special items become invisible (because they were already visible))
  • So, IsVisible is false for the items, but Visibility = Visible. Which is a bit strange.
  • The problem seems to depend on the use of the _roots list, which in a certain way contain the _leafs. When I change the itemsSource to different lists with special items in it, everything works fine. The hierarchical structure of the _roots make this thing broken.

I hope that this is a complete overview of my problem. Help would be appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf