1

I have a Window, and there are three types of content that can be displayed (they are all of UserControl type):

  1. Login view
  2. App view
  3. Error reporting

What's the most appropriate way to switch between these? My thought was to pass instance of Window in the constructor and then addressing it's content.

Content = new LoginView(this);

And then change the content from LoginView,

public LoginView(Window wnd){
    InitializeComponents();
    wnd.Content = new MainView(wnd);
}

But this wouldn't update the Window's content. Is it caused by the caller (LoginView) being the window's current content? If that's the case, what would be the proper way to handle such situation?

Also note that the snippet provided doesn't include any logic. I just left it as simple as required to demonstrate the issue I'm facing.

Basically the connection between those controls is such:

  1. Login view - when the application starts - when the application window sends a request (to the server) that returns unauthorized

  2. App view - handles all the application's features

  3. Error view - replaces app/login view in case of an error and informs user about what to do

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pikausp
  • 1,142
  • 11
  • 31
  • [Consider using](https://wpftoolkit.codeplex.com/wikipage?title=Wizard) [a Wizard](http://www.codeproject.com/Articles/31837/Creating-an-Internationalized-Wizard-in-WPF) [control of sorts](http://msdn.microsoft.com/en-us/library/aa972123(v=vs.85).aspx) :p – Kilazur Aug 29 '14 at 13:13
  • The idea is correct but you should handle "navigation" from the outside, not in the controls constructors. – Pragmateek Aug 29 '14 at 13:14
  • What relationship do these forms share? it sounds like the app form should control opening the other forms – Sayse Aug 29 '14 at 13:16
  • @Pragmateek Can you elaborate please? Not quite sure what you meant by "from the outside". Please note that the content switching doesn't actually take place in the constructor. As I already mentioned, I wanted to use as least of code as possible for the snippet. – pikausp Aug 29 '14 at 13:25
  • Do you want to follow the MVVM pattern? – AxdorphCoder Aug 29 '14 at 13:33
  • 1
    Look at this answer http://stackoverflow.com/a/25543066/3636642 – AxdorphCoder Aug 29 '14 at 13:53
  • 1
    @AxdorphCoder thanks for the link axdorph, it clarified some questions I had left after reading the accepted answer. – pikausp Aug 29 '14 at 14:04

1 Answers1

2

You should maybe look at the MVVM pattern. Usually every user control / view should have its own view model, when using the MVVM pattern. So you can define different Views inside a MainView. Like the following example shows:

<Window.Resources>
    <DataTemplate x:Key="LoginView" DataType="{x:Type ViewModel:LoginViewModel}">
            <local:LoginView />
    </DataTemplate>
    <DataTemplate x:Key="AppView" DataType="{x:Type ViewModel:AppViewModel}">
            <local:AppView />
    </DataTemplate>
<Window.Resources>

So in your code of the MainView, you have to pass the appropriate ViewModel in a ContentControl. This will reference the right View.

<ContentControl Content="{Binding LoginViewModel}" />

So the DataTemplate will be shown, depending on the xxxViewModel that is passed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Watta
  • 61
  • 4