WPF中 INotifyPropertyChanged

在Windows Presentation Foundation (WPF)中,INotifyPropertyChanged 是一个核心接口,用于实现实体类与视图之间的数据双向绑定 。当实体类的某个属性值 发生变化时,通过实现此接口可以立即通知绑定到该属性的所有 UI 控件进行更新,ICommand主要针对的是关联到任何实现了 ICommand 接口的对象的方法。

在C#中,CallerMemberName 是.NET框架提供的一个编译器特性(Compiler Feature),它允许你获取调用当前方法的成员名称,而无需硬编码该名称。这对于实现INotifyPropertyChanged接口特别有用,因为它可以减少手动输入属性名的工作量,提高代码的健壮性和可维护性。

不管是ICommand还是INotifyPropertyChanged 都必须首先将ViewMode的实例设置为控件或整个界面的 DataContext如,this.DataContext = new MainViewModel();DataContext是UI层与数据逻辑层的桥梁。

DataContext作为一个容器,提供了UI层和数据层之间的连接点。在MVVM(Model-View-ViewModel)架构模式中,通常将 ViewModel 设置为控件或整个界面的 DataContext,这样 UI 控件可以通过绑定直接访问 ViewModel 中的数据和命令。

例如,在上面的INotifyPropertyChanged实现中,我们可以使用CallerMemberName特性来简化OnPropertyChanged方法的调用:

cs 复制代码
1using System.ComponentModel;
2using System.Runtime.CompilerServices;
3
4public class MyViewModel : INotifyPropertyChanged
5{
6    // ...
7
8    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
9    {
10        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
11    }
12}
13
14public string MyProperty
15{
16    get { return myPropertyBackingField; }
17    set
18    {
19        if (myPropertyBackingField != value)
20        {
21            myPropertyBackingField = value;
22            OnPropertyChanged(); // 这里不再需要传入 "MyProperty"
23        }
24    }
25}

在此版本中,当你调用OnPropertyChanged()时,编译器会自动填充propertyName参数,将其设为调用方法的成员名称,即"MyProperty"。这样,当MyProperty的值改变时,绑定系统能够准确地知道哪个属性发生了变化并作出相应的更新。

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class MyNotifyProperyChanged : INotifyPropertyChanged //主要通过实现INotifyPropertyChanged接口,实现UI和属性绑定
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string propertyname ="")//属性值变化调用事件,属性特性要调用的属性值给propertyname
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyname));
        }

    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplication1
{
    public class MainViewModel : MyNotifyProperyChanged
    {
        public MyCommand MyCommandOrder { get; set; }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged();//调用基类事件
            }
        }

        private string _tittle;

        public string Tittle
        {
            get { return _tittle; }
            set { _tittle = value; OnPropertyChanged(); }
        }
        public MainViewModel()
        {
            MyCommandOrder = new MyCommand(ShowMessage);
        }

        public void ShowMessage()
        {
            Name = "点击了按钮";
            Tittle = "我是标题";
            MessageBox.Show(Name);
        }
    }
}
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }
}
XML 复制代码
<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBox  Text="{Binding Name}"> </TextBox>
            <TextBox  Text="{Binding Tittle}" ></TextBox>
            <Button Content="按钮" Command="{Binding MyCommandOrder}"></Button> <!--  Command命令绑定,绑定的也是一个属性,需要指定数据源,实现ICommand接口 -->
        </StackPanel>
    </Grid>
</Window>
相关推荐
Java Fans10 小时前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽18 小时前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf
code bean2 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go2 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19722 天前
自定义事件wpf
wpf
code bean2 天前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静3 天前
WPF可拖拽ListView
c#·wpf
界面开发小八哥3 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon4 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars6 天前
WPF中自定义消息弹窗
wpf