WPF INotifyPropertyChanged使用方法_苏守坤的博客-程序员秘密

技术标签: WPF  

扣扣技术交流群:460189483 

INotifyPropertyChanged 接口:向客户端发出某一属性值已更改的通知。

NotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

一般使用地方是:加载数据时,及时更新相应的数据加载名称。操作功能时,及时提示相应的错误信息。

实例:

xaml代码:

 <TextBlock Margin="80,5,80,0" TextWrapping="Wrap" Foreground="White" FontFamily="微软雅黑" Name="txtInfo" 
Text="{Binding Message, Mode=TwoWay}" ToolTip="{Binding Message}" TextTrimming="WordEllipsis" Grid.Row="2" FontSize="14"></TextBlock>

后台代码:

private string _message = string.Empty;
/// <summary>
/// 错误消息
/// </summary>
public string Message
{
    get { return _message; }
    set
    {
        _message = value;
        //使用时用Message才能反应到控件中,直接给_message赋值不能直接反应到控件中
        NotifyPropertyChanged("Message");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

消息赋值:

   Message = "正在加载数据!";

 

详细实例(抄袭):

在WPF中进行数据绑定的时候常常会用到INotifyPropertyChanged接口来进行实现,下面来看一个INotifyPropertyChanged的案例。

下面定义一个Person类:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.ComponentModel;  
  
namespace WpfApp  
{  
    public class Person:INotifyPropertyChanged  
    {  
        private String _name = "张三";  
        private int _age = 24;  
        private String _hobby = "篮球";  
            
        public String Name  
        {  
            set  
            {  
                _name = value;  
                if (PropertyChanged != null)//有改变  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));//对Name进行监听  
                }  
            }  
            get  
            {  
                return _name;  
            }  
        }  
  
        public int Age  
        {  
            set  
            {  
                _age = value;  
                if (PropertyChanged != null)  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));//对Age进行监听  
                }  
            }  
            get  
            {  
                return _age;  
            }   
        }  
        public String Hobby//没有对Hobby进行监听  
        {  
            get { return _hobby; }  
            set { _hobby = value; }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
    }  
}  

上面定义的这个Person类中,对Name和Age属性进行了监听,但是没有对Hobby进行监听。

 

MainWindow.xmal界面文件定义的内容如下:

<Window x:Class="WpfApp.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="300" Width="350">  
    <Grid Name="grid">   
        <TextBox Height="20" Text="{Binding Path=Name}"  HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />  
        <TextBox Height="20"  Text="{Binding Path=Age}"  HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />  
        <TextBox Height="20" Text="{Binding Path=Hobby}"  HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />  
          
        <Button Content="显示用户信息" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />  
        <Button Content="修改用户信息" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />  
  
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1"   Text="{Binding Path=Name}"  VerticalAlignment="Top" Width="88" />  
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />  
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />  
    </Grid>  
</Window>  

后台代码是:

using System;  
using System.Collections.Generic;  
using System.Linq;  
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 WpfApp  
{  
    /// <summary>  
    /// MainWindow.xaml 的交互逻辑  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        public MainWindow()  
        {  
            InitializeComponent();  
        }  
  
        private Person p1 = new Person();  
        private void button1_Click(object sender, RoutedEventArgs e)  
        {  
            grid.DataContext = p1;//绑定数据  
            p1.Name = "李四";   
            p1.Hobby = "足球";  
        }   
        private void button2_Click(object sender, RoutedEventArgs e)  
        {     
            p1.Age = p1.Age + 1;  
            p1.Hobby = "足球";  
        }  
    }  
}  

当点击显示用户数据的时候

下面看看这些信息具体都来自于哪儿?

由于在Person中没有对Hobby进行监听,所以p1.Hobby="足球"这个语句没有起到作用。 点击修改用户信息的时候也是不能修改绑定到界面上的对应Hobby的信息(即使是在界面处写了Mode=TwoWay,也是不能进行绑定的)。

所以使用INotifyPropertyChanged的时候,需要对要进行绑定的属性进行显示的设置的,否则绑定的时候是不能进行双向绑定的,即绑定是无效的。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014453443/article/details/89710251

智能推荐

Sql Server2008之关于”***对象无效“错误_躬匠的博客-程序员秘密

我们在sql server2008中编写sql语言时,经常会遇到这样的提示:***对象无效。这是什么错误,为什么在2000版本中就不存在这样的情况呢?其实这主要是sql server 2005/2008版本中新增了架构的概念。下面就结合网上的查询说一下我对sql server2008中架构的理解。可能有一些东西理解的不到位,欢迎各位指出,共同学习、改进。

thinkphp3.2.3集成phpexcel1.8导出设置单元格合并_weixin_30642305的博客-程序员秘密

thinkphp3.2.3集成phpexcel1.8导出设置单元格合并 1 到这里下载classes里面的文件https://github.com/PHPOffice/PHPExcel2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexc...

Yolov3/Yolov4原理对比改进创新_yolov4比yolov3改进的地方_酸辣土豆丝不要辣的博客-程序员秘密

YoLoV3原理详解Yolo的整个网络,吸取了Resnet、Densenet、FPN的精髓,可以说是融合了目标检测当前业界最有效的全部技巧。一、backbone主网络1、升级为Darknet-53yolov3的backbone部分由Yolov2时期的Darknet-19进化至Darknet-53,加深了网络层数,引入了Resnet中的跨层加和操作,达到了resNet-152的精度,却更快了两倍。对比如下:2、Darknet-53的网络结构共计大概53层,个人感觉就是resnet的轻微改版。

OMV搭建系列教程[1] – Debian9安装OpenMediaVault_openmediavault教程_NOOBYY的博客-程序员秘密

OMV搭建系列教程[1] – Debian9安装OpenMediaVaultOMV搭建系列教程[0] – 最小化安装Debian9 OMV搭建系列教程[1] – Debian9安装OpenMediaVault OMV搭建系列教程[2] – 安装omv-extras OMV搭建系列教程[3] – 共享文件夹SMB设置 OMV搭建系列教程[4] – 安装Docker容器 OMV搭建系...

随便推点

旋转数组中的最小数字 java_小小白的成长之路的博客-程序员秘密

旋转数组中的最小数字 java题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。代码1:import java.util.ArrayList;publ...

Java -- springboot 配置 ckfinder_com.ckfinder_@程序员的博客-程序员秘密

基于 SpringBoot 下配置 ckfinder,提供图片的上传、选取等资源管理功能

【小睿精选·第七期】基于STM32的DIY蓝牙机械键盘_RTThreadIoTOS的博客-程序员秘密

【小睿精选】【小睿精选】第七弹来啦,本期共收录 6条 嵌入式资讯信息,希望可以帮到你。欢迎大家在文末留言,唠一唠你关注的话题,说不定下期就有你想要的惊喜!资讯类1、瑞萨推出RZ/V系列微...

MySQL半同步--after_flush_yzs87的博客-程序员秘密

简介在主库semisync加载或初始化时,调用函数semi_sync_master_plugin_init,为transaction_delegate、binlog_storage_delegate、binlog_transmit_delegate增加observer,分别对应plugin的变量为trans_observer、storage_observer、transmit_observer。这

Ubuntu 16.04安装Eclipse + C/C++开发环境配置_colin_lisicong的博客-程序员秘密

1 安装Eclipse 在Terminal中输入以下命令测试是否安装了Eclipse:eclipse如果没有安装,系统会提示你使用什么命令去安装eclipse,如下命令即可安装所需要的JDK等其他依赖关系:sudo apt install eclipse-platform此时安装完的Eclipse应该是默认隐藏了菜单栏的,参考Ubuntu中Eclipse的菜单栏显示问题即可。sudo gedit

CProgressCtrl 进度条的使用_cprogressctrl 使用_夏卡罗的博客-程序员秘密

进度条介绍  “进度条控件”是一个窗口,应用程序可以使用这个窗口来表明一个冗长操作的进度。它由一个从左到右,用系统高亮色逐渐填充的矩形组成。  CProgressCtrl类提供了Windows通用进度条控件的机能。这个控件(也就是CProgressCtrl类)只有对运行在Windows 95和Windows NT 3.51或更高版本下的程序才是有效的。  进度条控件具有一个范围和一个当