WPF MVVM INotifyPropertyChanged

本文详细介绍WPF MVVM(Model-View-ViewModel)模式的应用,包括界面属性绑定、INotifyPropertyChanged刷新机制,以及通过实例演示如何使用UserControl和ViewModelBase类实现数据与视图的双向绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍 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>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值