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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:53:59+00:00 2026-05-22T19:53:59+00:00

I use a treeview in WPF and I don’t want to lose the state

  • 0

I use a treeview in WPF and I don’t want to lose the state (expanding and focus) when I reload the binded collection.

At the first load it’s easy to expand just the first node, i use the following code :

    private void ExpandFirstNodeTree()
    {
        foreach (var item in TreeviewModel.Items)
        {
            TreeViewItem itm = (TreeViewItem)TreeviewModel.ItemContainerGenerator.ContainerFromItem(item);
            if (itm == null) continue;
            itm.IsExpanded = true;
        }
    }

I use a DependencyProprety to select an item. I explore the TreeView, find the TreeViewItem and set the item "IsSelected" property to true.

private static readonly DependencyProperty SelectedEntityCodeProperty =
        DependencyProperty.Register(PropertyHelper.GetName((EntitiesTreeview e) => e.SelectedEntityCode), typeof (string), typeof (EntitiesTreeview));

    public string SelectedEntityCode
    {
        get { return (string) GetValue(SelectedEntityCodeProperty); }
        set { SetValue(SelectedEntityCodeProperty, value); }
    }

    public EntitiesTreeview()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(EntitiesTreeview_Loaded);
    }

    private void LoadSelectedItem()
    {
        if ((!string.IsNullOrEmpty(SelectedEntityCode))
            && (TreeviewEntity.SelectedItem == null))
            ChangeSelectedItem<ENTITY>(SelectedEntityCode, TreeviewEntity);
    }

    private bool ChangeSelectedItem<T>(string entityCode, ItemsControl itemsControl) where T : ENTITYBASE
    {
        if (itemsControl != null)
        {
            foreach (var item in itemsControl.Items)
            {
                var currentContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if ((currentContainer != null)
                    && (item is T)
                    && ((item as T).CCODE == entityCode))
                {
                    currentContainer.IsSelected = true;
                    var selectMethod = typeof (TreeViewItem).GetMethod("Select", BindingFlags.NonPublic | BindingFlags.Instance);
                    selectMethod.Invoke(currentContainer, new object[] {true});
                    return true;
                }
                if (ChangeSelectedItem<T>(entityCode, currentContainer))
                    return true;
            }
        }
        return false;
    }

My problem is, when I reload the items collection, the focus is lost (selected item) and expanded items are collapsed. How can I separate bound items and ui ?
Last point, I want to set a selected item programmatically. How can I reload the selected item when the dependency property changed ?


I’ve already had a look to the josh smith solution ( http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx ) but I don’t want to use a ViewModel collection for my binding. I have different object type to bind and use ViewModel will be to painfull… IMO 🙂

  • 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-22T19:54:00+00:00Added an answer on May 22, 2026 at 7:54 pm

    I have different object type to bind and use ViewModel will be to painfull… IMO

    In fact, the problem you’re experiencing right now demonstrates exactly why not using a view model is too painful.

    If you construct a view model, you can implement IsSelected and IsExpanded properties in the view model class. You can then bind the relevant properties of the TreeViewItem to them. Once you do this, changes to the state in the UI will be reflected in the view model data, and if you reload the UI from the data, the changes in state will be retained.

    This is the simplest possible way to achieve what you’re trying to achieve. As an additional benefit, you can discard every last scrap of the mystifyingly hard-to-test code-behind you’ve posted above.

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

Sidebar

Related Questions

I have a hierarchical treeview in WPF. I use http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/ for eventbinding with the
I want in the window of a wpf to set a treeview and make
I want to use TreeView to browse folders and when I selected some folders
I am trying to use WPF databinding features to get a TreeView to display
i use jquery treeview http://docs.jquery.com/Plugins/Treeview/treeview i want to catch click on text node and
I've got a problem. I use TreeView in my WPF project to visualize my
is it possible to use two different DataTemplates in one WPF TreeView? Maybe the
I'm working with application, which use TreeView. I want some nodes have checkBoxes, but
I dont want to use asp.net's TreeView control. I want to create a custom
I would like to use standard icons in treeview but I am not sure

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.