0

I'm extremely new to WPF and the MVVM ideology, and am in the process of creating an app with it. So to my understanding, from reading this WPF-MVVM Pattern Guide, we have:

  1. a Window, which facilitates the navigation between Views via Relay Commands,
  2. some Views, which are XAML files that, when interacted with in a certain way (like pressing a button), uses a Command to call a method in
  3. our ModelViews, which handle most of the logic of the View.

To switch from View1 to View2, in View 1 we might have a Command attached to a button that tells us to load View2,

VIEW1

    <StackPanel>
           Button<
                Command="{Binding DoSomeWorkInModalView1}"
           />
    </StackPanel>

    <StackPanel Visibility="{Binding VariableInModalView1ThatNeedsToBeSet, Converter={StaticResource BooleanToVisibilityConverter}}">
           Button<
                Command="{Binding MethodThatLoadsView2}"
           />
    </StackPanel>

MODALVIEW1

    DoSomeWorkInModalView1(){
         //Doing some work here...
         VariableInModalView1ThatNeedsToBeSet = true;
    }

or something to that effect. After we press a button, ViewModal1 does some work and sets VariableInModalView1ThatNeedsToBeSet to true, so we can see our button and can now press it to go to View2. Great, all of this works in my application.

What I need to do is when this StackPanel becomes visible, call MethodThatLoadsView2 without having to press a button. In other words, as soon as VariableInModalView1ThatNeedsToBeSet is set to true, we load View2 instead of waiting on the user to press a button, kind of like how in React, useEffect() can be used to perform some logic as soon as the component is loaded.

Sorry if this is a little vague, let me know if you have any questions. Thanks!

wotocem
  • 141
  • 1
  • 1
  • 5
  • Normally when you want to display pages in your app you take something like And when you change the CurrentViewModel property in your code behind another page is displayed. To be able to show Viewmodels you need to define DataTemplates that convert your CurrentViewModel to a view. – Klamsi Oct 01 '20 at 06:43
  • The recommended pattern is known as View Model First. See this short example: [C# WPF Page Navigation](https://stackoverflow.com/a/61323201/3141792). – BionicCode Oct 01 '20 at 07:56
  • It's unfortunate that the author of that post chose to call his usercontrols pages. Seeing as how there's a wpf thing called a page. – Andy Oct 01 '20 at 13:13
  • @wotocem: So why don't you just call `MethodThatLoadsView2` just after you have set the `VariableInModalView1ThatNeedsToBeSet` property? – mm8 Oct 01 '20 at 15:04

0 Answers0