WPF-MVVM练习案例(适合学习)

一、效果图

在这里插入图片描述
通过+10,-10,0按钮,对文本框数据进行修改。

二、代码

Model层

myModel.cs

   class myModel
    {
        private int num=0;
        public int Num
        {
            get { return num; }
            set
            {
                num = value;
                
            }
        }
    }

ViewModel层

1.ViewModelBase.cs

using System.ComponentModel; //添加命名空间

namespace myWpf_command.ViewModel
{   //继承INotifyPropertyChanged接口
    class ViewModelBase:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyname)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    }
}

  1. Delegatecommand.cs
using System.Windows.Input;

namespace myWpf_command.ViewModel
{
    class Delegatecommand:ICommand
    {

        //定义两个个委托
        private Action<object> executeCommand;

        public Action<object> ExecuteCommand
        {
            get { return executeCommand; }
            set { executeCommand = value; }
        }

        private Func<object, bool> canExecuteCommand;

        public Func<object, bool> CanExecuteCommand
        {
            get { return canExecuteCommand; }
            set { canExecuteCommand = value; }
        }

        //有参数的构造函数
        public Delegatecommand(Action<object> excuteCommand, Func<object, bool> canExcuteCommand)
        {
            this.executeCommand = excuteCommand;
            this.canExecuteCommand = canExcuteCommand;
        }
        public bool CanExecute(object parameter)
        {
            if (CanExecuteCommand != null)
            {
                return this.CanExecuteCommand(parameter);
            }
            else
            {
                return true;
            }
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.ExecuteCommand != null)
            {
                this.ExecuteCommand(parameter);
            }
        }
        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteCommand != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}

3.CaculatorViewModelcs.cs

using System.Windows.Input;

namespace myWpf_command.ViewModel
{
    class CaculatorViewModelcs:ViewModelBase
    {
        private int num1;
        public int Num1
        {
            get { return num1; }
            set
            {
                num1 = value;
                this.RaisePropertyChanged("Num1");
            }
        }

        //基本方法
        public void Add(object param)
        {
            Num1 = Num1 + 10;
        }
        public void Reduce(object param)
        {
            Num1 = Num1 - 10;
        }
        public void Rest(object param)
        {
            Num1 =0;
        }
       
        //命令
        public ICommand AddCommand { get; set; }
        public ICommand ReduceCommand { get; set; }
        public ICommand ResetCommand { get; set; }

        //构造函数
        public CaculatorViewModelcs()
        {
         
           // Num1 = mymodel.Num;
            var model = new Model.myModel();
            num1 = model.Num;
            AddCommand = new Delegatecommand(Add, null);
            ReduceCommand = new Delegatecommand(Reduce, null);
            ResetCommand = new Delegatecommand(Rest, null);
        }

     

    }
}

View层

MainWindow.xaml

 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="148*"/>
            <ColumnDefinition Width="185*"/>
            <ColumnDefinition Width="184*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="81*"/>
            <RowDefinition Height="108*"/>
            <RowDefinition Height="131*"/>
        </Grid.RowDefinitions>
        <Button Content="+10" HorizontalAlignment="Left" Margin="63,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Height="22" Command="{Binding AddCommand}"/>
        <Button Content="-10" Grid.Column="2" HorizontalAlignment="Left" Margin="46,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Command="{Binding ReduceCommand}" />
        <Button Content="0" Grid.Column="1" HorizontalAlignment="Left" Margin="61,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Command="{Binding ResetCommand}"/>
        <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="29" Margin="0,40,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Num1}" VerticalAlignment="Top" Width="185" IsReadOnly="True"/>
        <Label Content="WPF MVVM 练习" Grid.Column="1" HorizontalAlignment="Left" Margin="36,45,0,0" VerticalAlignment="Top"/>

    </Grid>

MainWindow.xaml.cs

     public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel.CaculatorViewModelcs();
        }
    }

新手学习,还请各路大神多多指导~~~~~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值