Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Treeview Expand only a first node and selected item?

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 :)

like image 260
rad Avatar asked May 24 '11 17:05

rad


1 Answers

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.

like image 191
Robert Rossney Avatar answered Oct 01 '22 08:10

Robert Rossney