WPF数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<TextBox Text="{Binding Name}" Height="23" HorizontalAlignment="Left" Margin="122,68,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Age}" Height="23" HorizontalAlignment="Left" Margin="122,124,0,0" Name="txtAge" VerticalAlignment="Top" Width="120" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="82,71,0,0" Name="textBlock1" Text="姓名" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="82,127,0,0" Name="textBlock2" Text="年龄" VerticalAlignment="Top" />
<Button Content="Age++" Height="23" HorizontalAlignment="Left" Margin="262,71,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="显示Age" Height="23" HorizontalAlignment="Left" Margin="262,124,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>

MainWindow.xaml.cs

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 WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Person p1 = new Person();
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
p1.Name = "李大钊";
p1.Age = 28; txtName.DataContext = p1;
txtAge.DataContext = p1;
} private void button1_Click(object sender, RoutedEventArgs e)
{
p1.Age++;
} private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show((p1.Age).ToString());
}
}
}

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; namespace WpfApplication1
{
/// <summary>
/// INotifyPropertyChanged接口是向client发出某一属性值已经更改的通知
/// INotifyPropertyChanged是.net内置的接口,数据绑定DataContext是否实现了INotityPropertyChanged接口,假设实现了,就会监听PropertyChanged得知属性的变化
/// 假设要求后台对象的值发送改变,界面的值也跟着变,则须要实现INotityPropertyChanged接口。而且在对象属性值变化后触发事件
/// 假设说后台对象的值会不变,则没有必要实现这个接口
/// </summary>
public class Person:INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
} set
{
this.name = value;
if (PropertyChanged != null)
{ //假设Name属性发生了改变,则触发这个事件
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
} private int age;
public int Age
{
get
{
return age;
} set
{
this.age = value;
//假设有人(数据绑定的对象来监听的)监听这个事件(假设有人监听就是不等于null,假设没人监听这个事件就等于null)
if (PropertyChanged != null)
{
//假设Age属性发生了改变,则触发这个事件
PropertyChanged(this,new PropertyChangedEventArgs("Age"));
}
}
} public event PropertyChangedEventHandler PropertyChanged; }
}

INotitypropertyChanged的更多相关文章

  1. WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)

    WPFS数据绑定(要是后台类对象的属性值发生改变,通知在"client界面与之绑定的控件值"也发生改变须要实现INotitypropertyChanged接口) MainWindo ...

  2. WPF入门:数据绑定

    上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityPropertyChanged接口的 ...

随机推荐

  1. 廖雪峰Java14Java操作XML和JSON-2JSON-1Json介绍

    JSON是一种类似JavaScript对象的数据表示格式 JavaScript Object Notation 去除了JavaScript的执行语句 仅保留数据 JSON格式: 仅保留UTF-8编码 ...

  2. 前端CSS样式操作

    目录 字体和文字 设置标签的宽高 字体属性 文字的属性 文字对齐 text-align 文字装饰 text-decoration 首行缩进 text-indent 背景属性 背景图片 边框 画圆 di ...

  3. JS的高阶函数

    JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数. 这就是最简单的高阶函数啦 functio ...

  4. 关于promise的用法

    promise是一个对象,里面保存着某个未来才会结束的事件,通常是一个异步事件. promise对象的两个特点: 1.对象的状态不受外界影响:pending(进行中) fulfilled(已成功) r ...

  5. 密码学笔记(5)——Rabin密码体制和语义安全性

    一.Rabin密码体制 Rabin密码体制是RSA密码体制的一种,假定模数$n=pq$不能被分解,该类体制对于选择明文攻击是计算安全的.因此,Rabin密码体制提供了一个可证明安全的密码体制的例子:假 ...

  6. PAT甲级——A1006 Sign In and Sign Out

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  7. 数据挖掘-diabetes数据集分析-糖尿病病情预测_线性回归_最小平方回归

    # coding: utf-8 # 利用 diabetes数据集来学习线性回归 # diabetes 是一个关于糖尿病的数据集, 该数据集包括442个病人的生理数据及一年以后的病情发展情况. # 数据 ...

  8. 在AlexNet中LRN 局部响应归一化的理

    在AlexNet中LRN 局部响应归一化的理 一.LRN技术介绍: Local Response Normalization(LRN)技术主要是深度学习训练时的一种提高准确度的技术方法.其中caffe ...

  9. Leetcode152. Maximum Product Subarray乘积的最大子序列

    给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2 ...

  10. /etc/vimrc配置

    [root@guolicheng ~]# cat /etc/vimrc if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" s ...