Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVVM在WPF中导航视图_C#_Wpf_Mvvm - Fatal编程技术网

C# 使用MVVM在WPF中导航视图

C# 使用MVVM在WPF中导航视图,c#,wpf,mvvm,C#,Wpf,Mvvm,我试图在单击树项目时在窗口中显示视图,但该视图未显示 当前我正在单击Registration treeview项,然后我想在UserControl中显示RegistrationView 我正在添加我的代码。主窗口具有Treeview和Usercontrol <Window x:Class="SchoolProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x

我试图在单击树项目时在窗口中显示视图,但该视图未显示

当前我正在单击Registration treeview项,然后我想在UserControl中显示RegistrationView

我正在添加我的代码。主窗口具有Treeview和Usercontrol

<Window x:Class="SchoolProject.MainWindow"
    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:SchoolProject"
    xmlns:vm="clr-namespace:SchoolProject.ViewModel"
    xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:v="clr-namespace:SchoolProject.Views"
    xmlns:m="clr-namespace:SchoolProject.Model"
    mc:Ignorable="d"
    Title="MainWindow">
<Window.DataContext>
    <vm:MainWindowViewModel/>
</Window.DataContext>
<Window.Resources>
    <DataTemplate x:Key="RegisterTemplate" DataType="{x:Type vm:RegistrationViewModel}">
        <v:RegistrationView/>
    </DataTemplate>
    <DataTemplate x:Key="PersonalTemplate" DataType="{x:Type vm:PersonalInformationViewModel}">
        <v:AdminView/>
    </DataTemplate>
</Window.Resources>
<DockPanel>
    <Label Content="School Project" DockPanel.Dock="Top" Height="50"/>
    <StackPanel DockPanel.Dock="Left" Width="170">
        <TreeView ItemsSource="{Binding UserTypesList}" x:Name="Menu">
            <e:Interaction.Triggers>
                <e:EventTrigger EventName="SelectedItemChanged">
                    <e:InvokeCommandAction Command="{Binding SelectedItemChangedCmd}"
                                           CommandParameter="{Binding ElementName=Menu,Path=SelectedItem}"/>
                </e:EventTrigger>
            </e:Interaction.Triggers>
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type m:UserTypes}" ItemsSource="{Binding UserActionsList}">
                    <Label Content="{Binding UserType}"/>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type m:UserActions}">
                    <Label Content="{Binding UserAction}"/>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </StackPanel>
    <ContentControl x:Name="Views" Content="{Binding SelectedViewModel}"/>
</DockPanel>
</Window>
MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using SchoolProject.Model;
using SchoolProject.ViewModel.Commands;
using SchoolProject.Views;
namespace SchoolProject.ViewModel
{
    class MainWindowViewModel : BaseINPC
    {
        public object Views { get; set; }

        private object _selectedViewModel;
        public object SelectedViewModel
        {
            get
            {
                return _selectedViewModel;
            }
            set
            {
                _selectedViewModel = value;
                RaisePropertyChanged("SelectedViewModel");
            }
        }
        public List<UserTypes> UserTypesList { get; set; }

        private UserTypes _studentUserType, _facultyUserType, _adminUserType;

        public RelayCommand SelectedItemChangedCmd { get; private set; }

        public MainWindowViewModel()
        {
            _studentUserType = new UserTypes("Student");
            _studentUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Personal Information"),
                new UserActions("Fees Details"),
                new UserActions("Course Information")
            };

            _facultyUserType = new UserTypes("Faculty");
            _facultyUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Faculty Details"),
                new UserActions("Salary Details"),
                new UserActions("Course Details")
            };

            _adminUserType = new UserTypes("Admin");
            _adminUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Registration"),
                new UserActions("Reset Password"),  
                new UserActions("Fees")
            };

            UserTypesList = new List<UserTypes>();
            UserTypesList.Add(_studentUserType);
            UserTypesList.Add(_facultyUserType);
            UserTypesList.Add(_adminUserType);

            SelectedItemChangedCmd = new RelayCommand(SelectedItemChanged);
        }

        private void SelectedItemChanged(object args)
        {
            Type t = args.GetType();
            if (t.ToString() == "SchoolProject.Model.UserActions")
            {
                UserActions action = (UserActions)args;
                switch (action.UserAction)
                {
                    case "Personal Information":
                    {
                        SelectedViewModel = new PersonalInformationViewModel();
                        break;
                    }
                    case "Fees Details":
                    {
                        SelectedViewModel = new FeesDetailsViewModel();
                        break;
                    }
                    case "Course Information":
                    {
                        SelectedViewModel = new CourseInformationViewModel();
                        break;
                    }
                    case "Facutly Details":
                    {
                        SelectedViewModel = new FacultyDetailsViewModel();
                        break;
                    }
                    case "Salary Details":
                    {
                        SelectedViewModel = new SalaryDetailsViewModel();
                        break;
                    }
                    case "Course Details":
                    {
                        SelectedViewModel = new CourseDetailsViewModel();
                        break;
                    }
                    case "Registration":
                    {
                        SelectedViewModel = new RegistrationViewModel();
                        Views = new RegistrationView();
                        break;
                    }
                    case "Reset Password":
                    {
                        SelectedViewModel = new ResetPasswordViewModel();
                        break;
                    }
                    case "Fees":
                    {
                        SelectedViewModel = new FeesViewModel();
                        break;
                    }
                }
            }
        }
    }
}
class RegistrationViewModel
{
    public RegistrationViewModel
    {
    }
}
注册ViewModel.cs

using System;
using System.Collections.Generic;
using SchoolProject.Model;
using SchoolProject.ViewModel.Commands;
using SchoolProject.Views;
namespace SchoolProject.ViewModel
{
    class MainWindowViewModel : BaseINPC
    {
        public object Views { get; set; }

        private object _selectedViewModel;
        public object SelectedViewModel
        {
            get
            {
                return _selectedViewModel;
            }
            set
            {
                _selectedViewModel = value;
                RaisePropertyChanged("SelectedViewModel");
            }
        }
        public List<UserTypes> UserTypesList { get; set; }

        private UserTypes _studentUserType, _facultyUserType, _adminUserType;

        public RelayCommand SelectedItemChangedCmd { get; private set; }

        public MainWindowViewModel()
        {
            _studentUserType = new UserTypes("Student");
            _studentUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Personal Information"),
                new UserActions("Fees Details"),
                new UserActions("Course Information")
            };

            _facultyUserType = new UserTypes("Faculty");
            _facultyUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Faculty Details"),
                new UserActions("Salary Details"),
                new UserActions("Course Details")
            };

            _adminUserType = new UserTypes("Admin");
            _adminUserType.UserActionsList = new List<UserActions>()
            {
                new UserActions("Registration"),
                new UserActions("Reset Password"),  
                new UserActions("Fees")
            };

            UserTypesList = new List<UserTypes>();
            UserTypesList.Add(_studentUserType);
            UserTypesList.Add(_facultyUserType);
            UserTypesList.Add(_adminUserType);

            SelectedItemChangedCmd = new RelayCommand(SelectedItemChanged);
        }

        private void SelectedItemChanged(object args)
        {
            Type t = args.GetType();
            if (t.ToString() == "SchoolProject.Model.UserActions")
            {
                UserActions action = (UserActions)args;
                switch (action.UserAction)
                {
                    case "Personal Information":
                    {
                        SelectedViewModel = new PersonalInformationViewModel();
                        break;
                    }
                    case "Fees Details":
                    {
                        SelectedViewModel = new FeesDetailsViewModel();
                        break;
                    }
                    case "Course Information":
                    {
                        SelectedViewModel = new CourseInformationViewModel();
                        break;
                    }
                    case "Facutly Details":
                    {
                        SelectedViewModel = new FacultyDetailsViewModel();
                        break;
                    }
                    case "Salary Details":
                    {
                        SelectedViewModel = new SalaryDetailsViewModel();
                        break;
                    }
                    case "Course Details":
                    {
                        SelectedViewModel = new CourseDetailsViewModel();
                        break;
                    }
                    case "Registration":
                    {
                        SelectedViewModel = new RegistrationViewModel();
                        Views = new RegistrationView();
                        break;
                    }
                    case "Reset Password":
                    {
                        SelectedViewModel = new ResetPasswordViewModel();
                        break;
                    }
                    case "Fees":
                    {
                        SelectedViewModel = new FeesViewModel();
                        break;
                    }
                }
            }
        }
    }
}
class RegistrationViewModel
{
    public RegistrationViewModel
    {
    }
}

发布问题时请注意格式。没有人想通读格式不好的代码。发布问题时请注意格式。没有人愿意通读格式不好的代码。
class RegistrationViewModel
{
    public RegistrationViewModel
    {
    }
}