WPF MVVM模式

#MainWindow.xaml
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="登录注册" Height="300" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="1" Grid.Column="0">
            <TextBlock Text="登录/注册" FontSize="22" HorizontalAlignment="Center" Margin="10"></TextBlock>
        </StackPanel>
        <Grid Grid.Row="2" HorizontalAlignment="Center" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="用户名" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"/>
            <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="1" Margin="2" VerticalContentAlignment="Center"/>

            <TextBlock Text="密码" Grid.Row="1" VerticalAlignment="Center"/>
            <TextBox Text="{Binding Password}" Grid.Row="1" Grid.Column="1" Margin="2" VerticalContentAlignment="Center"/>
            <CheckBox Grid.ColumnSpan="2" Content="记住密码" Grid.Row="2"/>
            <Button Grid.Row="3" Grid.ColumnSpan="2" Content="登录" Command="{Binding LoginAction}"/>
        </Grid>
    </Grid>
</Window>
#MainWindow.xamml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        LoginVM loginVm;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new LoginVM(this);
        }
        /**
         * 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //string userName = txtUserName.Text;
            //string password = txtPassword.Text;
            if (loginVm.UserName == "wpf" && loginVm.Password == "666")
            {
                //MessageBox.Show("OK");
                index index = new index();
                index.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("输入的用户名或者密码错误");
                loginVm.UserName = "";
                loginVm.Password = "";
            }
        }
        */
    }
}
#LoginVM.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
    class LoginVM: INotifyPropertyChanged
    {
        //把主线程MainWindow拿过来
        private MainWindow _main;
        public LoginVM(MainWindow main)
        {
            _main = main;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        //数据模型
        //背后大老板(私有的外部不可以直接访问)
        private LoginModel _LoginM = new LoginModel();
        
        //典当行伙计(窗口1转发)
        public string UserName
        {
            get { return _LoginM.UserName; }
            set {
                _LoginM.UserName = value;
                //通知到前台
                RaisePropertyChanged("UserName");
            }
        }

        //典当行伙计(窗口2转发)
        public string Password
        {
            get { return _LoginM.Password; }
            set
            {
                _LoginM.Password = value;
                RaisePropertyChanged("Password");
            }
        }
        //点击事件
        void LoginFunc() {
            if (UserName == "admin" && Password == "123456")
            {
                //MessageBox.Show("OK");
                index index = new index();
                index.Show();
                _main.Hide();
            }
            else
            {
                MessageBox.Show("输入的用户名或者密码错误");
                UserName = "";
                Password = "";
            }
        }
        bool CanLoginExecute()
        {
            return true;
        }
        public ICommand LoginAction
        {
            get
            {
                return new RelayCommond(LoginFunc, CanLoginExecute);
            }
        }
    }
}
#LoginModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1
{
    class LoginModel
    {
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        private string _Password;
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }
    }
}
#RelayCommond.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfApp1
{
    public class RelayCommond:ICommand
    {
        //命令是否能够执行
        readonly Func<bool> _canExecute;
        //命令需要执行的方法
        readonly Action _execute;
        public RelayCommond(Action action,Func<bool> canExecute)
        {
            _execute = action;
            _canExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            if(_canExecute == null)
            {
                return true;
            }
            return _canExecute();
        }
        public void Execute(object parameter)
        {
             _execute();
        }
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }
    }
}
#index.xaml
<Window x:Class="WpfApp1.index"
        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:WpfApp1"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="index" Height="600" Width="800">
    <Grid>
        <Grid Grid.Row="4" Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="60"></RowDefinition>
                <RowDefinition Height="50"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Text="用户名" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="1" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="3" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="0" Grid.Column="4" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="5" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="0" Grid.Column="6" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="7" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="1" Grid.Column="1" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="1" Grid.Column="3" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="1" Grid.Column="4" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="1" Grid.Column="5" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <TextBlock Text="用户名" Grid.Row="1" Grid.Column="6" VerticalAlignment="Center"/>
                <TextBox Text="{Binding UserName}" Grid.Row="1" Grid.Column="7" Margin="2" Width="90" VerticalContentAlignment="Center"/>
                <Button Grid.Row="1" Grid.Column="9" Click="Button_Click">检索</Button>
            </Grid>
            <Grid Grid.Row="1" Height="30" Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Horizontal">
                    <Button Width="80">添加</Button>
                    <Button Width="80">编辑</Button>
                    <Button Width="80">导入</Button>
                    <Button Width="80">导出</Button>
                    <Button Width="80">删除</Button>
                </StackPanel>
            </Grid>
            <Grid Grid.Row="2" Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition>
                    </RowDefinition>
                </Grid.RowDefinitions>
                <DataGrid>
                    <DataGrid.Columns>
                    <DataGridTemplateColumn  Width="55" Header="序号"  >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="状态" Visibility="Collapsed" Binding="{Binding DrugID}" IsReadOnly="True" />
                    <DataGridTextColumn Header="登记时间" Binding="{Binding DrugName}" IsReadOnly="True" />
                    <DataGridTextColumn Header="医院" Binding="{Binding count}" Width="50" IsReadOnly="False"/>
                    <DataGridTextColumn Header="姓名" Binding="{Binding DrugUnitName}" IsReadOnly="True"/>
                    <DataGridTextColumn Header="性别" Binding="{Binding RetailPrice}" Width="60" IsReadOnly="True"></DataGridTextColumn>
                    <DataGridTextColumn Header="年龄" Binding="{Binding RetailMoney}"  IsReadOnly="True"/>
                    <DataGridTextColumn Header="电话" Binding="{Binding DrugCode}"  IsReadOnly="True"/>
                    <DataGridTextColumn Header="地区" Binding="{Binding Specification}" IsReadOnly="True" />
                    <DataGridTextColumn Header="咨询时间" Binding="{Binding InventoryCount}" Width="80" IsReadOnly="True"/>
                    <DataGridTextColumn Header="到诊时间" Binding="{Binding Manufacturers}" Width="80" IsReadOnly="False" />
                </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </Grid>
    </Grid>
</Window>
#index.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
    /// <summary>
    /// index.xaml 的交互逻辑
    /// </summary>
    public partial class index : Window
    {
        public index()
        {
            InitializeComponent();
            this.Title = "医疗录入系统首页";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("class");
        }

    }
}
posted @ 2023-10-26 08:57  窦戈  阅读(17)  评论(0编辑  收藏  举报