none
WPF Switching Views preserving data

    Question

  • I want to have a [main view] with two buttons that loads [View 1] [View 2]

    View 1 has a some controls and View 2 has some controls

    I want to navigate pressing the buttons to switch the Views but instead of recreating the view I want to preserve the data

    for example if Im in View 1 and type: My Name is... inside the textbox 1, then if I want to go to other view and later return to view 1 keep the same data that I type previously

    of course I could have a Datagrid With filters in a view and I would like to keep that filter parameters or other kind of data

    Saturday, May 18, 2019 6:40 PM

All replies

  • Google viewmodel first.

    Here's a simple version:

    https://social.technet.microsoft.com/wiki/contents/articles/52485.wpf-tips-do-not-use-frame-and-page-for-navigation.aspx

    Extend the principles in that sample by caching a reference to each viewmodel.

    Either in an ioc container with a singleton setting.

    Or.

    In a dictionary, keyed by type.

    Then when you navigate resolve the viewmodel or check for an entry in the dictionary.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Saturday, May 18, 2019 7:21 PM
    Moderator
  • If the data is just being shown then that is easier but since you are also updating the data that is more complex.

    You can use ViewModel or something like that. There are abundant resources available for ViewModel and similar methodologies. Generally speaking, a critical design criteria is that your data should be read into and written from objects of the data separate from the forms. Microsoft began .Net using techniques where the data is part of the form; in the technology preceding .Net Microsoft's C++ design was a document/view architecture and the initial .Net designers did things differently. If the data for each form gets data from and puts data into the same objects and the reading and writing of that data is separate from the forms then you don't have the problem of preserving the data between switches.

    A tab control is one way to create buttons that switch views. You could use the button click event to update the other tab(s) when changes are made to a tab and the user switches views. The best solution is to use an ObservableCollection but as a beginner you might choose to use the first solution for now.



    Sam Hobbs
    SimpleSamples.Info



    Sunday, May 19, 2019 9:14 PM