Search code examples
wpfmvvm

Can you describe what is incorrect in my WPF project&


I'm new to WPF. This is my code for MainWindow

<StackPanel Grid.Row="1">

                <RadioButton Content="Главная"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"
                             IsChecked="True"
                             Command="{Binding ChangeToHome}"/>

                <RadioButton Content="Добавить раскраску"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding ChangeToDiscovery}"/>
</StackPanel>

and this code for MainViewModel.cs

private void ChangeToHome (object value)
{
    CurrentView = HomeVM;
    MessageBox.Show("Now in home", "alert", MessageBoxButton.OK, MessageBoxImage.Information);
}

private void ChangeToDiscovery (object value)
{
    CurrentView = DiscoveryVM;
    MessageBox.Show("Now in Discovery", "alert", MessageBoxButton.OK, MessageBoxImage.Information);
}

private bool CanChangeView(object value)
{
    return true;
}


public MainViewModel() 
{
    HomeVM = new HomeViewModel();
    DiscoveryVM = new DiscoveryViewModel();
    CurrentView = HomeVM;

    HomeViewCommand = new RelayCommand(ChangeToHome, CanChangeView);

    DiscoveryViewCommand = new RelayCommand(ChangeToDiscovery, CanChangeView);
}

I'm not getting any errors, but nothing works. When i click on radio button there are no messages and it doesn't change the view.


Solution

  • The first thing that caught my eye was that the commands were bound to the wrong properties that were declared in the ViewModel.

    enter image description here

    enter image description here

    There may be other errors, but to understand this you need more complete fragments of your code. Including how you instantiate the ViewModel and pass it to the Data Context.