MVVM示例

来源:互联网 发布:淘宝怎么看不到v等级了 编辑:程序博客网 时间:2024/05/21 02:52

//DelegateCommand(命令绑定类,实现:ICommand接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;


namespace WpfMVVM.Commands
{
    class DelegateCommand:ICommand
    {
        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc==null)
            {
                return true;
            }
            return this.CanExecuteFunc(parameter);
        }


        public void Execute(object parameter)
        {
            if (this.ExecuteAction==null)
            {
                return;
            }
            this.ExecuteAction(parameter);
        }
        public Func<object,bool> CanExecuteFunc { get; set; }//Func是一个委托,第一个类型是参数类型,第二个类型是返回值类型


        public Action<object> ExecuteAction { get; set; }//Acton也是一个委托,但他没有返回值,里面类型是参数的类型



        public event EventHandler CanExecuteChanged;
    }
}

//ViewModel基类(实现INotifyPropertyChanged接口,VeiwModel类继承该类,也可以ViewModel直接是实现该接口也可以)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;


namespace WpfMVVM.ViewModels
{
    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

//ViewModel类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using WpfMVVM.Commands;


namespace WpfMVVM.ViewModels
{
    class ViewModelMainClass : NotificationObject
    {
        private double input1;


        public double Input1
        {
            get { return input1; }
            set
            {
                input1 = value;
                this.RaisePropertyChanged("Input1");
            }
        }


        private double input2;


        public double Input2
        {
            get { return input2; }
            set
            {
                input2 = value;
                this.RaisePropertyChanged("Input2");
            }
        }


        private double result;


        public double Result
        {
            get { return result; }
            set
            {
                result = value;
                this.RaisePropertyChanged("Result");
            }
        }


        public DelegateCommand AddCommand { get; set; }


        private void Add(object parameter)
        {
            this.Result = this.Input1 + this.Input2;
        }


        public ViewModelMainClass()
        {
            this.AddCommand = new DelegateCommand();
            this.AddCommand.ExecuteAction=new Action<object>(this.Add);
        }
    }
}

//xaml代码

    <Grid>
        <TextBox Text="{Binding Input1}" x:Name="tb1" Height="60" Background="LemonChiffon" Margin="10,0,10,240"/>
        <TextBox Text="{Binding Input2}" x:Name="tb2" Height="60" Background="LemonChiffon" Margin="10,70,10,180" />
        <TextBox Text="{Binding Result}" x:Name="tb3" Height="60" Background="LemonChiffon" Margin="10,140,10,120"/>
        <Button x:Name="bt1" Command="{Binding AddCommand}" Margin="0,240,0,0">Add</Button>
    </Grid>

0 0
原创粉丝点击