0

I am creating a WPF application. It contains a window which is filled with a page. Within this page I have a treeview which acts as a menu for my application.

How would I be able to navigate to another page while keeping the treeview unchanged

eg. A users navigates to a page, when that page loads the treeview automatically resets, but I want the treeview to remain as the user left it(opened branches to the last menu item)

At the moment I have the following on each page:

This is why it resets

        <TreeView>
            <TreeViewItem Header="Items">
                <TreeViewItem Header="Item 1"></TreeViewItem>
                <TreeViewItem Header="Item 2"></TreeViewItem>
            </TreeViewItem>

            <TreeViewItem Header="ore Items">
                <TreeViewItem Header="Item a"></TreeViewItem>
                <TreeViewItem Header="Item b"></TreeViewItem>
            </TreeViewItem>
        </TreeView>

If the user clicks on Items>Item 1. When the next page loads I'd like the tree view to be opened as the user left it (Items>Item 1) and not reload to default.

Is this possible to do?

L.Persad
  • 47
  • 7
  • https://stackoverflow.com/questions/3989018/save-wpf-treeview-state-on-data-reload Look at @Rachels answer. – eran otzap Jul 04 '19 at 11:12

1 Answers1

0

Take the TreeView outside of the page so that it persists outside it when the page changes.

For example in your window you may have a grid where the TreeView is in row 0 and the Frame that hosts your pages is in row 1.

The other thing you can do is save the state of the TreeView to a value that is within the datacontext of all pages that use the TreeView. This will allow the state of the TreeView to be preserved between navigations.

kenjara
  • 402
  • 10
  • 18
  • the position of the treeview actually needs to be on the page to keep the structure – L.Persad Jul 04 '19 at 09:37
  • Can you elaborate why it must be in the page? – kenjara Jul 04 '19 at 09:39
  • Entire pages is a ribbon, and treeview sits under the ribbon on the left side. If I place the treeview out of the page then the ribbon does not fill the entire width of the page – L.Persad Jul 04 '19 at 09:42
  • @L.Persad Well that's something else entirely and to do with how your controls are styled. I have updated my answer with a way to preserve the state of the TreeView between page navigations. Do you have a TreeView on all Pages or is it when you come back to the one with the TreeView that you expect the state to be as you left it? – kenjara Jul 04 '19 at 09:48
  • I now have it in a user control and I make reference to that user control on each page, So I would like to reserve it on the user control which will allow it to be reserved on each page – L.Persad Jul 04 '19 at 10:06
  • Ok you are heading in the right direction then. Unfortunatly preserving the selected item in the TreeView control is not straight forward. I suggest reading up on this question https://stackoverflow.com/questions/1000040/data-binding-to-selecteditem-in-a-wpf-treeview – kenjara Jul 04 '19 at 10:17