WPF MVVM结构demo

1、项目结构如下图:

WPF MVVM结构demo_第1张图片

 2、加model代码如下:

 public class Animal
    {
        public string Name { get; set; }
        public string food { get; set; }
    }

3、添加Command代码如下: 

 public class ZooCommand : ICommand
    {
        private Action _execute;
        private Func _canExecute;

        public ZooCommand(Action execute, Func canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute();
        }
    }

 4:添加ViewModel代码如下:

 class MainViewModel : INotifyPropertyChanged
    {
        private string _searchText = "素食";

        private string name;

        private string food;

        private ObservableCollection animals;

        private ObservableCollection animalsSource;

        public event PropertyChangedEventHandler PropertyChanged;

        //属性改变通知
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        //构造函数
        public MainViewModel()
        {
            animalsSource = new ObservableCollection();
            animalsSource.Add(new Animal() { Name = "熊猫", food = "素食", });
            animalsSource.Add(new Animal() { Name = "老虎", food = "肉食", });
            animalsSource.Add(new Animal() { Name = "豹子", food = "肉食", });
            animalsSource.Add(new Animal() { Name = "天鹅", food = "素食", });
            animalsSource.Add(new Animal() { Name = "猴子", food = "杂食", });
            animals = animalsSource;
        }

        //名字
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        //食物
        public string Food
        {
            get { return food; }
            set
            {
                food = value;
                OnPropertyChanged("Food");
            }
        }

        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                OnPropertyChanged("SearchText");
            }
        }

        public ObservableCollection ResultList
        {
            get { return animals; }
            set
            {
                animals = value;
                OnPropertyChanged("ResultList");
            }
        }

        public ICommand QueryCommand
        {
            get { return new ZooCommand(Searching, CanSearching); }
        }

        public ICommand AddCommand
        {
            get { return new ZooCommand(Add, CanSearching); }
        }

        public void Add()
        {
            if (this.name != null && this.food != null)
            {
                animalsSource.Add(new Animal() { Name = this.name, food = this.food });
                Console.WriteLine(animals.Count);
            }

        }

        public void Searching()
        {
            ObservableCollection tempList = null;
            if (string.IsNullOrWhiteSpace(SearchText))
            {
                ResultList = animalsSource;
            }
            else
            {
                tempList = new ObservableCollection();
                foreach (Animal p in animalsSource)
                {
                    if (p.food.Contains(SearchText))
                    {
                        tempList.Add(p);
                    }
                }
                if (tempList != null)
                {
                    ResultList = tempList;
                }
            }
        }

        public bool CanSearching()
        {
            return true;
        }

    }

5、View关联ViewModel


    
        
            
        
        
        

6、布局文件里面注意引入ViewModel的命名空间:  xmlns:local="clr-namespace:MVVMDemo.ViewModel" 

 

 

你可能感兴趣的:(WPF)