使用MVVM

时间:2017-09-04 18:22:06

标签: c# wpf mvvm

我正在尝试在单击树项目的窗口中显示视图,但视图未显示。

目前我点击Registration treeview Item然后我想在UserControl中显示RegistrationView。

我正在添加我的代码。 MainWindow有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>

MainWindow.xaml.cs

namespace SchoolProject
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
}

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;
                    }
                }
            }
        }
    }
}

我目前有近9种不同的观点,我正在研究注册视图。所以我正在添加注册视图的代码。

RegistrationView.xaml

     <UserControl x:Class="SchoolProject.Views.RegistrationView"
         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:vm="clr-namespace:SchoolProject.ViewModel"
         xmlns:c="clr-namespace:SchoolProject.ViewModel.Commands"
         xmlns:local="clr-namespace:SchoolProject.Views"
         mc:Ignorable="d">
<UserControl.Resources>
    <c:PasswordConverter x:Key="PassConverter"/>
    <vm:RegistrationViewModel x:Key="regvm"/>
</UserControl.Resources>
<UserControl.DataContext>
    <vm:RegistrationViewModel/>
</UserControl.DataContext>
<Grid>
   <Label Content="Registration View"/>
</Grid>
</UserControl>

RegistrationView.xaml.cs

public partial class RegistrationView : UserControl
{
    public RegistrationView()
    {
        InitializeComponent();
        this.DataContext = new RegistrationViewModel();
    }
}

RegistrationViewModel.cs

class RegistrationViewModel
{
    public RegistrationViewModel
    {
    }
}

0 个答案:

没有答案