WPF+MVVM实现datagrid绑定(1)

it2026-01-24  3

概念性的东西不做任何介绍,网上千篇一律,我们需要真正能实现的代码。网上的好些资料都是不全的,无法实现,为确保正常运行,先贴图证明。(本来想在一个写完,内容过多,一直卡)

启动

 

 

 

内容概述:

1、框架:

1、实体代码

很简单不介绍

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WPF_MVVM_Demo1.Model { public class User { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } public string Remarks { get; set; } } }

2、Viewmodel

1、BaseCommon类

我定义了两个,另一个是泛型类,为啥这样的我也没搞明白,修改的时候用到的是泛型类。

namespace WPF_MVVM_Demo1.ViewModel { public class BaseCommands:ICommand { public Action<object> ExecuteCommand = null; public Func<object, bool> CanExecuteCommand = null; //当命令可执行状态发生改变时,应当被激发 public event EventHandler CanExecuteChanged; //用于判断命令是否可以执行 public bool CanExecute(object parameter) { if (CanExecuteCommand != null) { return this.CanExecuteCommand(parameter); } else { return true; } } //命令执行 public void Execute(object parameter) { if (this.ExecuteCommand != null) this.ExecuteCommand(parameter); } } public class BaseCommands<T> : ICommand { readonly Action<object> _execute; readonly Predicate<object> _canExecute; public BaseCommands(Action<object> execute) : this(execute, null) { } public BaseCommands(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } } }

  源码

下一篇

 

最新回复(0)