C# 使用菜单项切换ContentControl

C# 使用菜单项切换ContentControl,c#,wpf,mvvm,C#,Wpf,Mvvm,我对WPF和MVVM非常陌生,并且绝望地迷失了方向。我试图创建一个简单的窗口,其中包含几个可通过菜单项切换的视图,但遇到了两个问题: 如何动态生成菜单项列表?(附图中的视图1、视图2、视图3) 如何向这些菜单项添加命令以更改视图 这是我当前生成窗口的代码。目前它不会更改视图。“我的主窗口”包含菜单栏和用于保存视图的ContentControl。ContentControl的内容旨在在名为UserControl1、UserControl2和UserControl3的三个UserControl之间

我对WPF和MVVM非常陌生,并且绝望地迷失了方向。我试图创建一个简单的窗口,其中包含几个可通过菜单项切换的视图,但遇到了两个问题:

  • 如何动态生成菜单项列表?(附图中的视图1、视图2、视图3)
  • 如何向这些菜单项添加命令以更改视图
  • 这是我当前生成窗口的代码。目前它不会更改视图。“我的主窗口”包含菜单栏和用于保存视图的ContentControl。ContentControl的内容旨在在名为UserControl1、UserControl2和UserControl3的三个UserControl之间切换

    <Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:View="clr-namespace:WpfApplication3.View"
        xmlns:ViewModel="clr-namespace:WpfApplication3.ViewModel"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <DataTemplate DataType="{x:Type ViewModel:UserControl1ViewModel}">
            <View:UserControl1/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModel:UserControl2ViewModel}">
            <View:UserControl2/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModel:UserControl3ViewModel}">
            <View:UserControl3/>
        </DataTemplate>
    </Window.Resources>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="22"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.ColumnSpan="2">
            <MenuItem Header="_File">
                <MenuItem Header="Quit" HorizontalAlignment="Left" Width="144.506666666667"/>
            </MenuItem>
            <MenuItem Header="_View">
                <MenuItem Header="View 1" HorizontalAlignment="Left" Width="144.506666666667"/>
                <MenuItem Header="View 2" HorizontalAlignment="Left" Width="144.506666666667"/>
                <MenuItem Header="View 3" HorizontalAlignment="Left" Width="144.506666666667"/>
            </MenuItem>
        </Menu>
        <ContentControl Content="{Binding View}" Grid.Row="1" Grid.ColumnSpan="2"/>
    </Grid>
    
    
    

    这是主窗口的WIP视图模型。我以教程为起点,试图修改它以满足我的需要

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Collections.ObjectModel;
    
    namespace WpfApplication3.ViewModel
    {
        public class MainWindowViewModel : ViewModelBase
        {
            private readonly ObservableCollection<ControlViewModelBase> views;
            private int selectedView;
    
            public ControlViewModelBase View
            {
                get { return views[selectedView]; }
            }
    
            public MainWindowViewModel()
            {
                selectedView = 0;
                this.views = new ObservableCollection<ControlViewModelBase>();
    
                views.Add(new UserControl1ViewModel());
                views.Add(new UserControl2ViewModel());
                views.Add(new UserControl3ViewModel());
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Threading.Tasks;
    使用System.Collections.ObjectModel;
    命名空间WpfApplication3.ViewModel
    {
    公共类MainWindowViewModel:ViewModelBase
    {
    私有只读可观察收集视图;
    私有int-selectedView;
    公共控制视图模型库视图
    {
    获取{返回视图[selectedView];}
    }
    公共主窗口视图模型()
    {
    selectedView=0;
    this.views=新的ObservableCollection();
    添加(新的UserControl1ViewModel());
    添加(新的UserControl2ViewModel());
    添加(新的UserControl3ViewModel());
    }
    }
    }
    
    我以前的尝试看起来非常像WinForms,并且有很多代码用于捕获菜单项单击和视图切换。我正在尝试使用MVVM和数据绑定来实现这一点,但由于我仍在尝试围绕这两个概念进行思考


    另外,如果您需要查看更多代码示例,请告诉我

    您能给我解释一下吗?不同的视图具有不同的逻辑,或与其他视图属性(如背景或字体系列等)具有相同的逻辑?感恩观有不同的逻辑。