C# MVVM,ICommand的简单实现

Model部分
实现INotifyPropertyChanged接口,使其能够发出某一属性值发生改变的通知

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

namespace MVVMTest.Model
{
    public class PersonModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public PersonModel()
        {
            Name = "name";
            Age = "20";
            Sex = "男";
        }

        public string name;
        public string Name
        {
            get {   return name;}
            set
            {
                name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name));  
            }
        }

        public string age;
        public string Age
        {
            get { return age; }
            set
            {
                age = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age"));
            }
        }

        public string sex;
        public string Sex
        {
            get { return sex; }
            set
            {
                sex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Sex"));
            }
        }
    }
}

ViewModel部分:
首先实现Icommand接口,包含一个Action和Func委托。

    public class DeleteCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public Action<T> _execute = null;
        public Func<T, bool> _canExecute = null ;

        
        public DeleteCommand(Action<T> execute ):this(execute,null)
        {
        }

        public DeleteCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            if (canExecute != null)
            {
                this._canExecute = new Func<T, bool>(canExecute);
            }
            if (execute != null)
            {
                this._execute = new Action<T>(execute);
            }
        }
        
        public void RaiseCanExceuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        public bool CanExecute(object parameter)
        {
            if (this._canExecute == null)
            {
                return true;
            }
            return _canExecute((T)(object)(parameter));
        }

        public void Execute(object parameter)
        {
            this._execute((T)(object)(parameter));
        }
    }
}

PersonViewModel类作为数据源,其中包含Model实例和一个DeleteCommand命令;

public class PersonViewModel
    {

    //    public delegate void Fun(PersonModel person);

        public PersonModel Model{ set; get; }
        public DeleteCommand<PersonModel> deleteCommand { set; get; }

        public PersonViewModel()
        {
            Model = new PersonModel();
            deleteCommand = new DeleteCommand<PersonModel>( ClearData ,IsPersonNotEmpty);
        }

        public void ClearData(PersonModel person)
        {
        //    PersonModel personModel = person as PersonModel;
            Model.Age = "";
            Model.Name = "";
            Model.Sex = "";
        }

        public bool IsPersonNotEmpty(PersonModel person)
        {
        //    PersonModel personModel = person as PersonModel;
        //    if (string.IsNullOrEmpty(personModel.Name) && string.IsNullOrEmpty(personModel.Age) && string.IsNullOrEmpty(personModel.Sex))
        //        return false;
            return true;
        }
    }

View部分

<Window x:Class="MVVMTest.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:MVVMTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="525">
    <Grid Margin="0,0,0,0">
        <StackPanel Margin="0,0,0,0">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                <TextBox  Width="100" Text="{Binding Model.Name}" Height="30" Margin="10" />
                <Button x:Name="btn" Content="Clear" Command="{Binding deleteCommand}" Width="100" Height="30" Margin="150,10,10,10"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Age" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center"  HorizontalAlignment="Center"/>
                <TextBox  Width="100" Text="{Binding Model.Age}" Height="30" Margin="10"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Sex" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                <TextBox  Width="100" Text="{Binding Model.Sex}" Height="30" Margin="10"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

程序运行:
在这里插入图片描述

点击Clear按钮
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38094489/article/details/84109425