【问题标题】:Navigation from one view to another in WPF MVVM在 WPF MVVM 中从一个视图导航到另一个视图
【发布时间】:2016-10-30 12:36:02
【问题描述】:

我编写了应该使用 MVVM 在 WPF 应用程序中的用户控件之间导航的代码,但我意识到这段代码不起作用。 从窗口LoginView 我想将视图更改为VotingCardView

实际上,点击LoginView 中的按钮后,方法DisplayVCV 被执行,但视图不会改变。我做错了什么?

MainView.xaml:

<Window x:Class="ElectionCalculator.View.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ElectionCalculator"
        xmlns:v="clr-namespace:ElectionCalculator.View"
        xmlns:vm="clr-namespace:ElectionCalculator.ViewModel"
        mc:Ignorable="d"
        Title="Election calculator" Height="350" Width="525">
    <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>
    <ContentControl Content="{Binding ViewModel}" />
</Window>

LoginView.xaml:

<UserControl x:Class="ElectionCalculator.View.LoginView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ElectionCalculator.View"
      xmlns:vm="clr-namespace:ElectionCalculator.ViewModel"

      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <Button Command="{Binding DataContext.DisplayVC, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=OneWay}" Margin="161,147,47,124" />
    </Grid>

</UserControl>

MainViewModel.cs

class MainViewModel : BaseViewModel
{
    public BaseViewModel ViewModel { get; set; }

    public MainViewModel()
    {
        ViewModel = new LoginViewModel();
    }

    public ICommand DisplayVC { get { return new RelayCommand(DisplayVCV); } }

    public void DisplayVCV()
    {
        ViewModel = new VotingCardViewModel();

        MessageBox.Show("DisplayVCCommandExecuted");
    }
}

【问题讨论】:

    标签: c# wpf xaml mvvm navigation


    【解决方案1】:

    当值更改时,您的 ViewModel 属性实现不会引发 PropertyChanged 事件。这通常通过INotifyPropertyChanged 实现来完成。因此,您的视图不会收到发生更改的通知。

    在您的情况下,这意味着您需要为您的 ViewModel 属性提供一个支持字段并实现类似于此的 ViewModel 属性:

    private BaseViewModel _viewModel;
    public BaseViewModel ViewModel
    {
      get { return _viewModel; }
      set
      {
        if(_viewModel != value) 
        {
          _viewModel = value;
          OnPropertyChanged("ViewModel");
        }
      }
    }
    

    由于您已经从BaseViewModel 派生,我假设方法OnPropertyChanged(或一些具有相似名称的方法)在那里实现。您不必指定属性名称 ("ViewModel") 作为参数也很常见,因为许多实现为此目的使用 [CallerMemberName] 属性。

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多