學習 wpf 如果不學 mvvm,仿佛缺少了靈魂。那什麼是 mvvm 呢?為什麼要學 mvvm 呢,本以一個簡單的增刪改查的小例子,簡述 mvvm 的基本知識及如何通過進行 mvvm 架構的程式開發,僅供學習分享使用,如有不足之處,還請指正。
什麼是 mvvm?
mvvm 是 model-view-viewmodel 的簡寫。它本質上就是 mvc (model-view- controller)的改進版。即模型-視圖-視圖模型。分別定義如下:
- 【模型】指的是後端傳遞的數據。
- 【視圖】指的是所看到的頁面。
- 【視圖模型】mvvm 模式的核心,它是連接 view 和 model 的橋樑。它有兩個方向:
- 一是將【模型】轉化成【視圖】,即將後端傳遞的數據轉化成所看到的頁面。實現的方式是:數據綁定。
- 二是將【視圖】轉化成【模型】,即將所看到的頁面轉化成後端的數據。實現的方式是:dom 事件監聽。這兩個方向都實現的,我們稱之為數據的雙向綁定。
mvvm 示意圖如下所示:

安裝 mvvmlight 插件
項目名稱右鍵-->管理 nuget 程式包-->搜索 mvvmlight-->安裝。如下所示:

彈出接受許可證窗口,點擊【接受】如下所示:

mvvmlight 安裝成功後,自動引用需要的第三方庫,並默認生成示例內容,有些不需要的需要刪除,如下所示:

mvvm 示例截圖
主要通過 mvvm 實現數據的 crud【增刪改查】基礎操作,如下所示:

mvvm 開發步驟
- 創建 model 層
本例主要是對學生信息的增刪改查,所以創建 student 模型類,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp3.Models
{
/// <summary>
/// 学生类
/// </summary>
public class Student
{
/// <summary>
/// 唯一标识
/// </summary>
public int Id { get; set; }
/// <summary>
/// 学生姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 班级
/// </summary>
public string Classes { get; set; }
}
}
- 創建 dal 層
為了簡化示例,模擬資料庫操作,構建基礎數據,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp3.Models;
namespace WpfApp3.DAL
{
public class LocalDb
{
private List<Student> students;
public LocalDb() {
init();
}
/// <summary>
/// 初始化数据
/// </summary>
private void init() {
students = new List<Student>();
for (int i = 0; i < 30; i++)
{
students.Add(new Student()
{
Id=i,
Name=string.Format("学生{0}",i),
Age=new Random(i).Next(0,100),
Classes=i%2==0?"一班":"二班"
});
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <returns></returns>
public List<Student> Query()
{
return students;
}
/// <summary>
/// 按名字查询
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public List<Student> QueryByName(string name)
{
return students.Where((t) => t.Name.Contains(name)).ToList();//FindAll((t) => t.Name.Contains(name));
}
public Student QueryById(int Id)
{
var student = students.FirstOrDefault((t) => t.Id == Id);
if (student != null)
{
return new Student() {
Id=student.Id,
Name=student.Name,
Age=student.Age,
Classes=student.Classes
};
}
return null;
}
/// <summary>
/// 新增学生
/// </summary>
/// <param name="student"></param>
public void AddStudent(Student student)
{
if (student != null)
{
students.Add(student);
}
}
/// <summary>
/// 删除学生
/// </summary>
/// <param name="Id"></param>
public void DelStudent(int Id)
{
var student = students.FirstOrDefault((t) => t.Id == Id); //students.Find((t) => t.Id == Id);
if (student != null)
{
students.Remove(student);
}
}
}
}
- 創建 view 層
view 層與用戶進行交互,用戶數據的展示,及事件的響應。在本例中,view 層主要有數據查詢展示,新增及編輯頁面。
在 view 層,主要是命令的綁定,及數據的綁定。
- 在 datagridtextcolumn 中通過 binding=""的形式綁定要展示的列屬性名。
- 在 button 按鈕上通過 command=""的形式綁定要響應的命令。
- 在 textbox 文本框中通過 text=""的形式綁定查詢條件屬性。
數據展示窗口,如下所示:
<Window
x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel
Orientation="Horizontal"
Grid.Row="0"
Margin="5"
VerticalAlignment="Center"
>
<TextBlock Text="姓名:" Margin="10" Padding="5"></TextBlock>
<TextBox
x:Name="sname"
Text="{Binding Search}"
Width="120"
Margin="10"
Padding="5"
></TextBox>
<button
x:Name="btnQuery"
Content="查询"
Margin="10"
Padding="5"
Width="80"
Command="{Binding QueryCommand}"
></button>
<button
x:Name="btnReset"
Content="重置"
Margin="10"
Padding="5"
Width="80"
Command="{Binding ResetCommand}"
></button>
<button
x:Name="btnAdd"
Content="创建"
Margin="10"
Padding="5"
Width="80"
Command="{Binding AddCommand}"
></button>
</StackPanel>
<DataGrid
x:Name="dgInfo"
Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserSortColumns="False"
Margin="10"
ItemsSource="{Binding GridModelList}"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="Id"
Width="100"
Binding="{Binding Id}"
></DataGridTextColumn>
<DataGridTextColumn
Header="姓名"
Width="100"
Binding="{Binding Name}"
></DataGridTextColumn>
<DataGridTextColumn
Header="年龄"
Width="100"
Binding="{Binding Age}"
></DataGridTextColumn>
<DataGridTextColumn
Header="班级"
Width="100"
Binding="{Binding Classes}"
></DataGridTextColumn>
<DataGridTemplateColumn Header="操作" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"
>
<button
x:Name="edit"
Content="编辑"
Width="60"
Margin="3"
Height="25"
CommandParameter="{Binding Id}"
Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
></button>
<button
x:Name="delete"
Content="删除"
Width="60"
Margin="3"
Height="25"
CommandParameter="{Binding Id}"
Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
></button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
新增及編輯頁面,如下所示:
<Window
x:Class="WpfApp3.Views.StudentWindow"
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:WpfApp3.Views"
mc:Ignorable="d"
Title="StudentWindow"
Height="440"
Width="500"
AllowsTransparency="False"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock FontSize="30" Margin="10">修改学生信息</TextBlock>
<StackPanel Grid.Row="1" Orientation="Vertical">
<TextBlock FontSize="20" Margin="10" Padding="5">姓名</TextBlock>
<TextBox
x:Name="txtName"
FontSize="20"
Padding="5"
Text="{Binding Model.Name}"
></TextBox>
<TextBlock FontSize="20" Margin="10" Padding="5">年龄</TextBlock>
<TextBox
x:Name="txtAge"
FontSize="20"
Padding="5"
Text="{Binding Model.Age}"
></TextBox>
<TextBlock FontSize="20" Margin="10" Padding="5">班级</TextBlock>
<TextBox
x:Name="txtClasses"
FontSize="20"
Padding="5"
Text="{Binding Model.Classes}"
></TextBox>
</StackPanel>
<StackPanel
Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Right"
>
<button
x:Name="btnSave"
Content="保存"
Margin="10"
FontSize="20"
Width="100"
Click="btnSave_Click"
></button>
<button
x:Name="btnCancel"
Content="取消"
Margin="10"
FontSize="20"
Width="100"
Click="btnCancel_Click"
></button>
</StackPanel>
</Grid>
</Window>
- 創建 viewmodel 層
viewmodel 層是 mvvm 的核心所在,起到承上啟下的作用。viewmodel 需要繼承 galasoft.mvvmlight.viewmodelbase 基類。
viewmodel 中屬性實現數據的綁定,命令實現用戶交互的響應。如下所示:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using WpfApp3.DAL;
using WpfApp3.Models;
using WpfApp3.Views;
namespace WpfApp3.ViewModel
{
/// <summary>
///
/// </summary>
public class MainViewModel : ViewModelBase
{
#region 属性及构造函数
private LocalDb localDb;
private ObservableCollection<Student> gridModelList;
public ObservableCollection<Student> GridModelList
{
get { return gridModelList; }
set
{
gridModelList = value;
RaisePropertyChanged();
}
}
/// <summary>
/// 查询条件
/// </summary>
private string search;
public string Search
{
get { return search; }
set
{
search = value;
RaisePropertyChanged();
}
}
/// <summary>
///
/// </summary>
public MainViewModel()
{
localDb = new LocalDb();
QueryCommand = new RelayCommand(this.Query);
ResetCommand = new RelayCommand(this.Reset);
EditCommand = new RelayCommand<int>(this.Edit);
DeleteCommand = new RelayCommand<int>(this.Delete);
AddCommand = new RelayCommand(this.Add);
}
#endregion
#region command
/// <summary>
/// 查询命令
/// </summary>
public RelayCommand QueryCommand { get; set; }
/// <summary>
/// 重置命令
/// </summary>
public RelayCommand ResetCommand { get; set; }
/// <summary>
/// 编辑
/// </summary>
public RelayCommand<int> EditCommand { get; set; }
/// <summary>
/// 删除
/// </summary>
public RelayCommand<int> DeleteCommand { get; set; }
/// <summary>
/// 新增
/// </summary>
public RelayCommand AddCommand { get; set; }
#endregion
public void Query()
{
List<Student> students;
if (string.IsNullOrEmpty(search))
{
students = localDb.Query();
}
else
{
students = localDb.QueryByName(search);
}
GridModelList = new ObservableCollection<Student>();
if (students != null)
{
students.ForEach((t) =>
{
GridModelList.Add(t);
});
}
}
/// <summary>
/// 重置
/// </summary>
public void Reset()
{
this.Search = string.Empty;
this.Query();
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="Id"></param>
public void Edit(int Id)
{
var model = localDb.QueryById(Id);
if (model != null)
{
StudentWindow view = new StudentWindow(model);
var r = view.ShowDialog();
if (r.Value)
{
var newModel = GridModelList.FirstOrDefault(t => t.Id == model.Id);
if (newModel != null)
{
newModel.Name = model.Name;
newModel.Age = model.Age;
newModel.Classes = model.Classes;
}
this.Query();
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="Id"></param>
public void Delete(int Id)
{
var model = localDb.QueryById(Id);
if (model != null)
{
var r = MessageBox.Show($"确定要删除吗【{model.Name}】?","提示",MessageBoxButton.YesNo);
if (r == MessageBoxResult.Yes)
{
localDb.DelStudent(Id);
this.Query();
}
}
}
/// <summary>
/// 新增
/// </summary>
public void Add()
{
Student model = new Student();
StudentWindow view = new StudentWindow(model);
var r = view.ShowDialog();
if (r.Value)
{
model.Id = GridModelList.Max(t => t.Id) + 1;
localDb.AddStudent(model);
this.Query();
}
}
}
}
- 數據上下文
當各個層分別創建好後,那如何關聯起來呢?答案就是 datacontext【數據上下文】。
查詢頁面上下文,如下所示:
namespace WpfApp3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainViewModel viewModel = new MainViewModel();
viewModel.Query();
this.DataContext = viewModel;
}
}
}
新增頁面上下文,如下所示:
namespace WpfApp3.Views
{
/// <summary>
/// StudentWindow.xaml 的交互逻辑
/// </summary>
public partial class StudentWindow : Window
{
public StudentWindow(Student student)
{
InitializeComponent();
this.DataContext = new
{
Model = student
};
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
}
總結
mvvm 具有低耦合,可重用,可測試,獨立開發的優點,核心要素就兩個:
- 屬性發生變化時的通知,即可達到數據的實時更新。
- 命令是實現用戶與程式之間數據和算法的橋樑。
備註
本文作為 mvvm 的簡單入門示例,旨在拋磚引玉,一起學習,共同進步。如果對 wpf 的其他入門知識,不是很了解,可以參考其他博文。
玉樓春·別後不知君遠近
歐陽修 〔宋代〕
別後不知君遠近,觸目淒涼多少悶。漸行漸遠漸無書,水闊魚沉何處問。
夜深風竹敲秋韻,萬葉千聲皆是恨。故攲單枕夢中尋,夢又不成燈又燼。註:攲(yǐ)
