代码之家  ›  专栏  ›  技术社区  ›  Artsiom Andros

你能描述一下我的WPF项目中有什么不正确的地方吗&

  •  0
  • Artsiom Andros  · 技术社区  · 1 年前

    我是WPF的新手。这是我为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>
    

    以及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);
    }
    

    我没有任何错误,但什么都不起作用。当我点击单选按钮时,没有消息,也不会改变视图。

    1 回复  |  直到 1 年前
        1
  •  0
  •   EldHasp    1 年前

    引起我注意的第一件事是,这些命令绑定到了ViewModel中声明的错误属性。

    enter image description here

    enter image description here

    可能还有其他错误,但要理解这一点,您需要更完整的代码片段。包括如何实例化ViewModel并将其传递给数据上下文。

    推荐文章