【问题标题】:WPF MVVM: MainWindow navigationWPF MVVM:主窗口导航
【发布时间】:2017-12-18 18:37:35
【问题描述】:

我有一个包含一个窗口的 WPF 应用程序。只有抛出这个窗口,用户才能在应用程序中执行他的导航。

应用结构为:

  • MainWindow.xaml
  • MainWindowViewModel.cs
  • StartPage.xaml
  • StartPageViewMode.cs
  • Systems.xaml
  • Systems.cs
  • 其他视图和相关视图模型。

MainWindow.xaml

<Grid>
   <ContentControl  Content="{Binding CurrentWorkspace}" x:Name="ContentControlMainWindow" VerticalAlignment="Stretch"/>
</Grid>

MainWindowViewModel.cs

private ContentControl _currentWorkspace;
public ContentControl CurrentWorkspace
{
     get => _currentWorkspace;
     set => SetProperty(ref _currentWorkspace, value);
}

//c'tor
public MainWindowViewModel()
{
   CurrentWorkspace.Content = new ContentControl { Content = new StartPage() 
}

如您所见,在应用程序初始化时,我将 StartPage 视图加载到 CurrentWorkspace。 现在从 StartPageViewModel 我需要将 CurrentWorkspace 内容更改为另一个视图。 基本上我很难从应用程序的每个部分控制(和更改)这个 CurrentWorkspace。

【问题讨论】:

  • 查看 WPF 中的导航概念。

标签: c# wpf mvvm binding contentcontrol


【解决方案1】:

我喜欢这种方式:

在 MainWindowViewModel.cs 中:

// You would more likely type this as something like ViewModelBase/ObservableObject/etc.
private object _currentWorkspace;
public object CurrentWorkspace
{
    get => _currentWorkspace;
    set => SetProperty(ref _currentWorkspace, value);
}

private StartPageViewModel _startPageViewModel;
public StartPageViewModel StartPageViewModel
{
    get => _startPageViewModel;
    set => SetProperty(ref _startPageViewModel, value);
}

private AnotherPageViewModel _anotherPageViewModel;
public AnotherPageViewModel AnotherPageViewModel
{
    get => _anotherPageViewModel;
    set => SetProperty(ref _anotherPageViewModel, value);
}

public MainWindowViewModel()
{   
    StartPageViewModel = new StartPageViewModel();
    AnotherPageViewModel = new AnotherPageViewModel();

    CurrentWorkspace = StartPageViewModel;
}

// Navigation Method
private void NavigateToStartPage()
{
    if (CurrentWorkspace != StartPageViewModel)
        CurrentWorkspace = StartPageViewModel;
}

// Navigation Method
private void NavigateToAnotherPage()
{
    if (CurrentWorkspace != AnotherPageViewModel)
        CurrentWorkspace = AnotherPageViewModel;
}

在 MainWindow.xaml 中:

<Window ...
    xmlns:vm="clr-namespace:App.ViewModels"
    xmlns:vw="clr-namespace:App.Views"
    ... >
    <Window.DataContext>
        <vm:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <ContentControl x:Name="ContentControlMainWindow"
                        Content="{Binding CurrentWorkspace}"
                        VerticalAlignment="Stretch">
            <ContentControl.Resources>
                <DataTemplate x:Key="start_page_view"
                              DataType="{x:Type vm:StartPageViewModel}">
                    <vw:StartPage />
                </DataTemplate>
                <DataTemplate x:Key="another_page_view"
                              DataType="{x:Type vm:AnotherPageViewModel}">
                    <vw:AnotherPage />
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>
    </Grid>
</Window/>

然后您可以将CurrentWorkspace 设置为您想要的任何值。如果你想处理StartPageViewModel的实例,你可以设置StartPageViewModel = null;

在您的 ViewModel 中包含 UI 元素(例如 ContentControl)通常被视为违反 MVVM。

【讨论】:

  • 谢谢克里斯,我仍然不明白如何从不同的视图模型(谁不是主窗口视图模型)更改当前工作区?
  • 我添加了另一个属性以及两个导航方法,希望可以解释这一点 - 然后是在MainWindowViewModel 上调用这些方法的问题,通常通过某种ICommand。让我知道这是否可以解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
相关资源
最近更新 更多