博客
关于我
INotifyPropertyChanged 接口
阅读量:415 次
发布时间:2019-03-06

本文共 4121 字,大约阅读时间需要 13 分钟。

INotifyPropertyChanged接口是.NET开发中一个核心的概念,用于在属性值发生更改时通知绑定客户端。这种机制特别有用在数据绑定场景中,确保UI或其他依赖的组件能够及时响应数据变化。

INotifyPropertyChanged的作用

该接口的核心功能是提供属性更改的通知。通过实现该接口,对象可以向其绑定的客户端(如UI控件或其他订阅者)发送通知,告知特定属性的值已发生变化。这对于保持UI的更新和用户体验至关重要。


如何实现INotifyPropertyChanged

要实现INotifyPropertyChanged,对象需要定义一个PropertyChanged事件,并在属性值更改时触发该事件。以下是实现步骤:

  • 定义事件:在类中定义public event PropertyChangedEventHandler PropertyChanged;
  • 提供通知方法:实现NotifyPropertyChanged方法,该方法将触发PropertyChanged事件。
  • 使用CallerMemberName属性:在NotifyPropertyChanged方法中使用[CallerMemberName]属性,可以让事件自动获取属性名称,无需手动传递。

  • 绑定类型的要求

    在数据绑定场景中,绑定类型需要具备以下功能之一:

    • 实现INotifyPropertyChanged接口:这是首选的实现方式,确保属性更改时自动触发通知。
    • 为每个属性提供更改事件:通过直接在属性的set块中调用NotifyPropertyChanged方法。
    • 不要同时实现上述两种功能:选择一种方式即可,避免冗余。

    代码示例

    以下代码展示了如何在C#中实现INotifyPropertyChanged接口。该示例使用了CallerMemberName属性,简化了通知方法的调用。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    namespace TestNotifyPropertyChangedCS
    {
    public partial class Form1 : Form
    {
    private Button changeItemBtn = new Button();
    private DataGridView customersDataGridView = new DataGridView();
    private BindingSource customersBindingSource = new BindingSource();
    private BindingList
    customerList = new BindingList
    ();
    public Form1()
    {
    InitializeComponent();
    this.changeItemBtn.Text = "Change Item";
    this.changeItemBtn.Dock = DockStyle.Bottom;
    this.changeItemBtn.Click += new EventHandler(changeItemBtn_Click);
    this.Controls.Add(this.changeItemBtn);
    customersDataGridView.Dock = DockStyle.Top;
    this.Controls.Add(customersDataGridView);
    this.Size = new Size(400, 200);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customersBindingSource.DataSource = customerList;
    customersDataGridView.DataSource = customersBindingSource;
    }
    void changeItemBtn_Click(object sender, EventArgs e)
    {
    customerList[0].CustomerName = "Tailspin Toys";
    customerList[0].PhoneNumber = "(708)555-0150";
    }
    }
    public class DemoCustomer : INotifyPropertyChanged
    {
    private Guid idValue = Guid.NewGuid();
    private string customerNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;
    public event PropertyChangedEventHandler PropertyChanged;
    [CallerMemberName]
    private void NotifyPropertyChanged([String] String propertyName = "")
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    private DemoCustomer()
    {
    customerNameValue = "Customer";
    phoneNumberValue = "(312)555-0100";
    }
    public static DemoCustomer CreateNewCustomer()
    {
    return new DemoCustomer();
    }
    public Guid ID
    {
    get { return this.idValue; }
    }
    public string CustomerName
    {
    get { return this.customerNameValue; }
    set
    {
    if (value != this.customerNameValue)
    {
    this.customerNameValue = value;
    NotifyPropertyChanged();
    }
    }
    }
    public string PhoneNumber
    {
    get { return this.phoneNumberValue; }
    set
    {
    if (value != this.phoneNumberValue)
    {
    this.phoneNumberValue = value;
    NotifyPropertyChanged();
    }
    }
    }
    }
    }

    总结

    INotifyPropertyChanged接口是.NET开发中一个关键的工具,用于实现属性更改的通知。在实际应用中,建议优先选择实现该接口的方式,以确保绑定客户端能够及时响应数据变化。此外,使用CallerMemberName属性可以简化代码,并提高开发效率。

    转载地址:http://pufkz.baihongyu.com/

    你可能感兴趣的文章
    Netty WebSocket客户端
    查看>>
    netty 主要组件+黏包半包+rpc框架+源码透析
    查看>>
    Netty 异步任务调度与异步线程池
    查看>>
    Netty中集成Protobuf实现Java对象数据传递
    查看>>
    Netty事件注册机制深入解析
    查看>>
    Netty原理分析及实战(四)-客户端与服务端双向通信
    查看>>
    Netty客户端断线重连实现及问题思考
    查看>>
    Netty工作笔记0006---NIO的Buffer说明
    查看>>
    Netty工作笔记0007---NIO的三大核心组件关系
    查看>>
    Netty工作笔记0011---Channel应用案例2
    查看>>
    Netty工作笔记0013---Channel应用案例4Copy图片
    查看>>
    Netty工作笔记0014---Buffer类型化和只读
    查看>>
    Netty工作笔记0020---Selectionkey在NIO体系
    查看>>
    Vue踩坑笔记 - 关于vue静态资源引入的问题
    查看>>
    Netty工作笔记0025---SocketChannel API
    查看>>
    Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
    查看>>
    Netty工作笔记0050---Netty核心模块1
    查看>>
    Netty工作笔记0057---Netty群聊系统服务端
    查看>>
    Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
    查看>>
    Netty工作笔记0063---WebSocket长连接开发2
    查看>>