WPF Treeview Expand only the first node and the selected item?

I use treeview in WPF and I don't want to lose state (extension and focus) when I reload the bound collection.

At the first boot, it is easy to deploy only 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 am using DependencyProprety to select an item. I study TreeView, find TreeViewItem and set the "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 that when the collection of elements is reloaded, the focus is lost (the selected element), and the expanded elements are collapsed. How can I separate related elements and ui? In the last paragraph, I want to set the selected item programmatically. How can I reload the selected item when the dependency property has changed?

I already looked at the josh smith solution ( http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx ), but I don't want to use the ViewModel collection for my binding. I have another type of object to bind and use the ViewModel will hurt ... IMO :)

Thank you very much

+4
source share
1 answer

I have another type of object to bind and use the ViewModel will hurt ... IMO

In fact, the problem you are currently facing shows why using a view model is too painful.

If you are creating a view model, you can implement the IsSelected and IsExpanded in the view model class. Then you can bind the corresponding TreeViewItem properties to them. Once you do this, the state changes in the user interface will be reflected in the data of the view model, and if you reload the user interface from the data, the state changes will be saved.

This is the easiest way to achieve what you are trying to achieve. As an added benefit, you can discard every last snippet of hoax code you mentioned above.

+7
source

All Articles