Retrieving values from views before switching to another view

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I am having some trouble figuring out the best approach to pass data around when switching windows/views.

On application start up, I first check if the application needs to be configured for the first time.

If it does, I create a new instance of a window called 'AutoSizeWindow' which just has a ContentControl and auto sizes to the contents height and width.

I then set the ContentControls content to a new instance of my 'SetupTypeView'.

From that view, the user selects a setup type from a combobox and hits the continue button.

The selected value from the combo box is bound back the SetupTypeViewModel.

Once that process has completed I then need to display my 'SystemSettingsView' within the original 'AutoSizeWindow' but need to pass through the selected setup type the user chose.

Should I be updating the view from my 'SetupTypeViewModel' or should I be doing this back on the applications entry point?

Also how can I pass the chosen setup type back to my app.xaml.cs so that the next view can be displayed based on that selection?

Appreciate any help.

app.xaml.cs
C#:
private void AppStart(object sender, StartupEventArgs e)
{
    //Set default theme colors.
    ThemeManager.ChangeAccentColor(Colors.DarkRed);

    //Check if first time setup. If so, run through setup process and then go back to start to re-process user info.
    if (RegistryManager.GetRegistrySetting("FirstTimeSetup") == "true" || RegistryManager.GetRegistrySetting("FirstTimeSetup") == string.Empty)
    {
        //Display setup type view.
        AutoSizeWindow wdw = new AutoSizeWindow();
        SetupTypeView viewSetupType = new SetupTypeView();
        viewSetupType.DataContext = new SetupTypeViewModel();
        wdw.cntMain.Content = viewSetupType;
        wdw.ShowDialog();

        //Check setup type.

    }
    else
    {
        //Validate user etc
        //Load main window and view
    }
}

Is there a way I can access the property from the SetupTypeViewModel once the dialog window has been closed?
 
Last edited:
I've managed to do currently be creating a static AppStates class that all views can update where data needs to be shared.

Not sure if this is best practise but it's only going to have limited usage.
 
The way to do this is create one instance of a model that is shared between the view models, thereby effectively by the views. The view tells the view model what to update in the model, and the model get updated. The view model reads values from the model for the view to display. So if there is only a single underlying model, then all the views are consistent. Also, view models can be shared by views. There is no rule that each view needs a unique type of view model, and a unique instance of a view model. You can create one instance of a view model (which is hooked up to that same instance of the model) and pass it to all the views that need it. This is where dependency injection comes in. It's the D in SOLID object orient programming principles.

I unfortunately, not a lot of MVVM tutorials and books dwell on what I wrote above regarding sharing models and view models. They sort of leave it to the reader to learn this for themselves or figure it out at common sense object oriented programming.
 
It sounds complicated, but you get a grasp of it after a while when you've write your pattern a few times...

Remember, its not always required to use the MVVM approach. MVVM is recommended when using WPF, but ultimately the requirements of your application should dictate which pattern you need. And remember, just because it's screamed at people all across the net that this is the "pertinent" pattern for WPF. I advise people to generally use MVVM in WPF too. You should be choosing a pattern that does what you want, and not choosing a pattern because it's the most "recommended" across the net. MVVM is mostly used in complex scenarios where multiple models have complex binding patterns.
 
So if you don't have multiple models complexly bind to your view(s), then you likely don't need WPF MVVM. And therefore can choose an alternative easier patter for whatever it is you are writing. ;)

Edit typo
 
Last edited:
Back
Top Bottom