背景:该功能为几乎所有系统开发都需要使用的功能,现提供简单的案例。
1、MyCommand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfApplication25_DataGrid.ViewModels
{
public class MyCommand:ICommand
{
private readonly Action _execute;
private readonly Func _canExecute;
private readonly Action
2、Notify
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication25_DataGrid.ViewModels
{
public abstract class Notify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
3、数据模型Person
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication25_DataGrid.Models
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
4、编辑EditPersonWindow.xaml
5、编辑EditPersonWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApplication25_DataGrid.ViewModels;
namespace WpfApplication25_DataGrid.Views
{
///
/// EditPersonWindow.xaml 的交互逻辑
///
public partial class EditPersonWindow : Window
{
public EditPersonWindow(EditPersonViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}
}
6、EditPersonViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApplication25_DataGrid.Models;
namespace WpfApplication25_DataGrid.ViewModels
{
public class EditPersonViewModel : Notify
{
public Person Person { get; set; }
public Action CloseAction { get; set; }
public MyCommand SaveCommand { get; set; }
public MyCommand CancelCommand { get; set; }
public EditPersonViewModel(Person person)
{
Person = person;
SaveCommand = new MyCommand(SavePerson);
CancelCommand = new MyCommand(Cancel);
}
private void SavePerson()
{
CloseAction?.Invoke(true);
}
private void Cancel()
{
CloseAction?.Invoke(false);
}
}
}
7、MainWindow.xaml
8.PersonViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfApplication25_DataGrid.Models;
using WpfApplication25_DataGrid.Views;
namespace WpfApplication25_DataGrid.ViewModels
{
public class PersonViewModel : Notify
{
public ObservableCollection Persons { get; set; }
public Person SelectedPerson { get; set; }
public ICommand AddCommand { get; set; }
public ICommand DeleteCommand { get; set; }
public ICommand UpdateCommand { get; set; }
public ICommand SearchCommand { get; set; }
///
/// 查询字段
///
private string selectTxt = "未登录";
public string SelectTxt
{
get { return selectTxt; }
set
{
selectTxt = value;
OnPropertyChanged();
}
}
public PersonViewModel()
{
Persons = new ObservableCollection
{
new Person { Id = 1, Name = "Alice", Age = 25 },
new Person { Id = 2, Name = "Bob", Age = 30 },
new Person { Id = 3, Name = "Charlie", Age = 35 }
};
AddCommand = new MyCommand(AddPerson);
DeleteCommand = new MyCommand(DeletePerson);
UpdateCommand = new MyCommand(UpdatePerson);
SearchCommand = new MyCommand(SearchPerson);
}
///
/// 增
///
///
private void AddPerson()
{
var newPerson = new Person();
var editViewModel = new EditPersonViewModel(newPerson);
var editWindow = new EditPersonWindow(editViewModel);
editViewModel.CloseAction = (result) =>
{
editWindow.DialogResult = result;
editWindow.Close();
};
if (editWindow.ShowDialog() == true)
{
newPerson.Id = Persons.Count + 1;
Persons.Add(newPerson);
}
}
///
/// 删
///
///
///
private bool CanDeletePerson()
{
return SelectedPerson != null;
}
///
/// 删除
///
///
private void DeletePerson()
{
if (SelectedPerson != null)
{
var result = MessageBox.Show($"确认要删除 {SelectedPerson.Name} 吗?", "删除确认", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
Persons.Remove(SelectedPerson);
}
}
}
///
/// 更新
///
///
private void UpdatePerson()
{
if (SelectedPerson != null)
{
var editViewModel = new EditPersonViewModel(SelectedPerson);
var editWindow = new EditPersonWindow(editViewModel);
editViewModel.CloseAction = (result) =>
{
editWindow.DialogResult = result;
editWindow.Close();
};
if (editWindow.ShowDialog() == true)
{
// 因为传递的是引用,修改已在窗口中完成
}
}
}
查询
private void SearchPerson()
{
string sss = SelectTxt;
if (sss!="")
{
var searchResult = new ObservableCollection();
foreach (var person in Persons)
{
if (person.Id==Convert.ToInt16(sss))
{
searchResult.Add(person);
}
}
Persons = searchResult;
OnPropertyChanged(nameof(Persons));
}
}
}
}