Click here to Skip to main content
15,943,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I hope I am allowed to ask this question here. I was wondering if you guys could help me set up a example base of what I am trying to do within WPF using MVVM. I have not used MVVM before and I am able to accomplish what I am trying to do using code behind and showing and hiding controls but I do not want to go that way.

My goal is to be able to switch views on a dialog with datatemplates. So for this example I am using materialdesign toolkit and creating a login dialog box when a button is clicked on the form.

So the login dialog will have a username and a passwordbox with a login button on it, now when you press login and if the passwordbox password = "password" then show a different view of the dialog for changing the password (nothing needs to be on it)else it shows the logged in view (Also nothing else needs to be on it).

I hope this makes sense and I hope I can get help on here for this situation. Thank you!

What I have tried:

So far I have only tried the code behind and hiding and showing controls which can get tiresome.
Posted
Updated 31-Oct-19 3:09am
v2

That's a poor design. Make the user click a "Change Password" button to change the password. When the button is clicked, display a new form for that purpose.

If you want to be clever, you can use a borderless non-movable non-resizable form that's centered on the parent, and it won't look like a new form is being displayed.
 
Share this answer
 
Comments
Knowledged 31-Oct-19 7:31am    
No it would show a change password dialog if the password == default password.
Assuming that you want to change initial password by using single window with different views...

Check this: Switching between xaml views in mvvm | technical-recipes.com[^]
 
Share this answer
 
Comments
Knowledged 4-Nov-19 15:14pm    
This is great, it has me able to switch between views but I would like a seperate dialog on each view and I have no clue how to fire a command when a view is loaded.
If you're using MVVM, one option that's worked for me is to use a combination of DataTemplate and a ContentControl.

The ContentControl's Content property is bound to a property on the viewmodel typed as object. That property gets set to an instance of a specific viewmodel class representing the page I want to display. The DataTemplate for that viewmodel class uses a UserControl to display the page.

For example:

MainWindow.xaml
XAML
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:views="clr-namespace:..."
    xmlns:vm="clr-namespace:..."
    ...
>
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:MainWindowLoadingViewModel}">
            <views:MainWindowLoadingView />
        </DataTemplate>
        
        <DataTemplate DataType="{x:Type vm:MainWindowAccessDeniedViewModel}">
            <views:MainWindowAccessDeniedView/>
        </DataTemplate>
        
        ...
    </Window.Resources>
    
    <ContentControl Content="{Binding Path=CurrentPage}" />
</Window>
MainWindowViewModel.cs
C#
public class MainWindowViewModel : ViewModelBase
{
    private object _currentPage = new MainWindowLoadingViewModel();
    
    public object CurrentPage
    {
        get { return _currentPage; }
        private set { SetProperty(ref _currentPage, value); }
    }
    
    private void OnLoaded()
    {
        if (!UserCanAccessApplication)
        {
            CurrentPage = new MainWindowAccessDeniedViewModel();
            return;
        }
        
        ...
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900