0

I have a simple login view with username and password and an other where I would like to access if the two is valid in the database. The problem is the main navigation is going on in the MainWindow.xaml with menuItems and in the MainViewModel where I instantiate the CurrentViewModel property. If I access this property from the MainViewModel it does nothing and does not sends the actually view selected, unless I change the DataContext in the LoginViewmodel to the mainviewmodel. How could I solve it? Sorry if it sounds stupid or simple but this is my first complex application. What am I doing wrong?

If I set the command in the login to handle it, it works but this is not where the process is going through.

This is the MainViewModel:

private ObservableObject _selectedViewModel;
 public ObservableObject SelectedViewModel { 
get { return _selectedViewModel; } 
set { SetProperty(ref _selectedViewModel, value); }
 }    

public IRelayCommand<object> UpdateViewCommand { get; set; }

    public MainViewModel()
    {
        UpdateViewCommand = new RelayCommand<object>(e => Execute(e));
        SelectedViewModel = App.Current.Services.GetService<LoginViewModel>();
    }


    public void Execute(object parameter)
    {
        switch (parameter.ToString())
        {
            case "LogIn":
                SelectedViewModel = App.Current.Services.
                GetRequiredService<LogInViewModel>();
                break;
            case "ShowShops":
                SelectedViewModel = App.Current.Services.
                GetRequiredService<ShowShopsViewModel>();
                break;
        }
    }

LogInViewModel:

private ILoginRepository<User> _userRepo;

        public IAsyncRelayCommand<User> LogIn { get; }

        public IAsyncRelayCommand UpdateViewCommand { get; }


        public LogInViewModel(ILoginRepository<User> userRepo)
        {
            _userRepo = userRepo;
            UpdateViewCommand = new AsyncRelayCommand<User>(e => LogIn(e), CanLogin);
        }

        public async Task LogIn(User user)
        {
            App.Current.Services.GetRequiredService<ShowShopsViewModel>();
        }
        public bool CanLogin(User user)
        {
            return true;
        }

MainWindow.xaml:


    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>
<Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

    <MenuItem Header="Show..."  Padding="10,0,10,0">
        <MenuItem Header="Devices" />
        <MenuItem Header="Shops" Command="{Binding UpdateViewCommand}" CommandParameter="ShowShops" />
        <MenuItem Header="Purchases" />
        <MenuItem Header="Logs" />
    </MenuItem>
<ContentControl Grid.Row="1" Content="{Binding SelectedViewModel}"/>

</Grid>

LoginView.xaml:

<UserControl>
.
.
.
.
<Button Style="{StaticResource LoginButton}" Command="{Binding UpdateViewCommand}" Content="Login" BorderBrush="Transparent" Background="Transparent" BorderThickness="0" Width="230" Height="25" >
</UserControl>
qkory
  • 9
  • 1
  • Does this answer your question? [WPF MVVM navigate views](https://stackoverflow.com/questions/19654295/wpf-mvvm-navigate-views) – Rekshino Mar 16 '23 at 20:15

2 Answers2

0

For change the view you need to set MainViewModel.SelectedViewModel to the new value.

The question is how you'll get an instance of MainViewModel, which being used as a MainWindow.DataContext. If you can access it like App.Current.Services.GetRequiredService<MainViewModel>();, what I doubt, then your login method can looks like:

public async Task LogIn(User user)
{
    App.Current.Services.GetRequiredService<MainViewModel>().SelectedViewModel = 
                App.Current.Services.GetRequiredService<ShowShopsViewModel>();
}

If not, then you can try to pass it as CommandParameter:

<Button Style="{StaticResource LoginButton}" Command="{Binding UpdateViewCommand}" Content="Login" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource AncestorType=local:MainWindow}}">

public async Task LogIn(object dc)
{
    (dc as MainViewModel).SelectedViewModel =
                App.Current.Services.GetRequiredService<ShowShopsViewModel>();
}
Rekshino
  • 6,954
  • 2
  • 19
  • 44
0

Finally after a few days managed to solve the problem, I've just had to add the datacontext in the VIEWS as:

DataContext = App.Current.Services.GetRequiredService<MainViewModel>();
qkory
  • 9
  • 1