本文介绍 wpf MVVM(Model-View- ViewModel ) 模式,以及界面 绑定属性, INotifyPropertyChanged的刷新功能 ,附加知识点类的继承,UserControl的使用。
使用开发工具 vs2019
1.创建wpf项目
2.项目名称 MvvmDemo
3.创建UserControl
在PersonUserControl中加入控件
1>xaml文件
<UserControl x:Class="MvvmDemo.PersonUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MvvmDemo"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<local:PersonViewModel></local:PersonViewModel>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30*"></RowDefinition>
<RowDefinition Height="30*"></RowDefinition>
<RowDefinition Height="30*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Width="200" Height="100" Text="{Binding Name}"></TextBlock>
<TextBlock Grid.Row="1" Width="200" Height="100" Text="{Binding Age}"></TextBlock>
<Button x:Name="UpdateBtn" Content="更新" Grid.Row="2" Height="100" Width="300" Click="UpdateBtn_Click"></Button>
</Grid>
</UserControl>
2> .cs文件
using System;
using System.Collections.Generic;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
namespace MvvmDemo
{
/// <summary>
/// PersonUserControl.xaml 的交互逻辑
/// </summary>
public partial class PersonUserControl : UserControl
{
private PersonViewModel m_personViewModel;
public PersonUserControl()
{
InitializeComponent();
m_personViewModel = base.DataContext as PersonViewModel;
}
private void UpdateBtn_Click(object sender, RoutedEventArgs e)
{
try
{
m_personViewModel.Name = "李四";
m_personViewModel.Age = 25;
}
catch(Exception error)
{
Console.WriteLine(error.Message);
}
}
}
}
4.添加Person类
using System;
using System.Collections.Generic;
using System.Text;
namespace MvvmDemo
{
class Person
{
private string m_name;
private int m_age;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
public int Age
{
get
{
return m_age;
}
set
{
m_age = value;
}
}
}
}
5. 添加ViewModelBase类继承INotifyPropertyChanged,实现刷新功能
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
namespace MvvmDemo
{
class ViewModelBase:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
6.添加 PersonViewModel类继承ViewModelBase
using System;
using System.Collections.Generic;
using System.Text;
namespace MvvmDemo
{
class PersonViewModel:ViewModelBase
{
private Person m_person;
public PersonViewModel ()
{
m_person = new Person() { Name = "张三",Age=12};
}
public String Name
{
get
{
return m_person.Name;
}
set
{
m_person.Name = value;
NotifyPropertyChanged("Name"); //监测Name是否变化
}
}
public int Age
{
get
{
return m_person.Age;
}
set
{
m_person.Age = value;
NotifyPropertyChanged("Age") ; //监测Age是否变化
}
}
}
}
7.将PersonUserControl 添加到MainWindow.xaml
<Window x:Class="MvvmDemo.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:MvvmDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:PersonUserControl></local:PersonUserControl>
</Grid>
</Window>