<Window x:Class="BindingDemo1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="254" Width="430" Loaded="Window_Loaded">
<Grid Name="gr">
<Button Content="name" Height="23" HorizontalAlignment="Left" Margin="48,65,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="48,12,0,0" Name="txtName" VerticalAlignment="Top" Width="333" />
<Button Content="age" Height="23" HorizontalAlignment="Left" Margin="48,169,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="48,116,0,0" Name="txtAge" VerticalAlignment="Top" Width="333" />
</Grid>
</Window>
using System.ComponentModel;

namespace BindingDemo1
{
class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private string name;
public string Name
{
get { return name; }
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
} private int age;
public int Age
{
get { return age; }
set
{
age = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}
}
}

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace BindingDemo1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Student stu = new Student();
public MainWindow()
{
InitializeComponent(); this.DataContext = stu; this.txtName.SetBinding(TextBox.TextProperty, new Binding("Name"));
this.txtAge.SetBinding(TextBox.TextProperty, new Binding("Age"));
} private void button1_Click(object sender, RoutedEventArgs e)
{
stu.Name += "testName";
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
stu.Age += 10;
stu.Name += "testName";
} private void button2_Click(object sender, RoutedEventArgs e)
{
stu.Age += 10;
}
}
}

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

使用方式(操作步骤):
1. 定义(类必须继承INotifyPropertyChanged接口)
2. 页面实例化
3. XAML UI 绑定
4. 其他 UI 改变其绑定值

注意:
以上操作可参考“实例一”中描述;
通知项属性用于不同UI中动态改变绑定信息;
INotifyPropertyChanged为接口,PropertyChanged为事件,在更改属性值时触发(PropertyChanged 事件
可以通过将 null或 String. Empty 用作 PropertyChangedEventArgs 中的属性名,指示该对象上的所有属性
都已更改);

WPF Demo13通知项属性+数据绑定(代码层)的更多相关文章

  1. WPF的依赖项属性

    WPF的依赖项属性 属性与事件是.NET抽象模型的核心部分.WPF使用了更高级的依赖项属性(Dependency Property)功能来替换原来.NET的属性,实现了更高效率的保存机制,还添加了附加 ...

  2. 迟到的 WPF 学习 —— 依赖项属性

    本章学习依赖项属性,英文原文 Dependency Property,它是传统 .Net Framework 属性的扩展,是 WPF 的专属,但所幸使用起来和传统属性几乎一样.WPF 元素所提供的大多 ...

  3. 学习WPF——初识依赖项属性

    入门 首先创建一个依赖项属性 然后绑定父容器的DataContext到这个依赖项的实例 接着绑定子元素的属性到依赖项属性(注意Button的Content属性) 程序最终的运行结果:   说明 首先是 ...

  4. 浅谈WPF依赖项属性

    浅谈WPF依赖项属性 0. 引言 依赖项属性虽然在使用上和CLR属性一样,但是它是WPF特有的,不同于CLR属性.只是封装为我们常用CLR的属性,在语法使用上和CLR属性一样.WPF中一些功能:动画, ...

  5. [转]WPF 依赖项属性

    from:http://blog.csdn.net/datoumimi/article/details/8033682 ps:环境限制,发的东西一长就会被拦截,所以删了一部分 UI软件中经常会用到大量 ...

  6. WPF教程四:字段、属性、依赖项属性的演变过程

    这个章节主要讲解属性是什么,为什么会演变出依赖项属性,依赖属性的优势是什么.以及如何更好的使用属性和依赖项属性. 一.属性 属性是什么. 翻了好几本C#的书和微软的文档,我觉得对属性讲解比较好理解的就 ...

  7. 如何在 WPF 中获取所有已经显式赋过值的依赖项属性

    原文:如何在 WPF 中获取所有已经显式赋过值的依赖项属性 获取 WPF 的依赖项属性的值时,会依照优先级去各个级别获取.这样,无论你什么时候去获取依赖项属性,都至少是有一个有效值的.有什么方法可以获 ...

  8. WPF中的依赖属性

    1. WPF中的依赖属性 依赖属性是专门基于WPF创建的.在WPF库实现中,依赖属性使用普通的C#属性进行了包装,使用方法与普通的属性是相同的. 1.1 依赖属性提供的属性功能 资源 数据绑定 样式 ...

  9. 【WPF学习笔记】之WPF基础:依赖关系属性和通知

    这些天来,对象似乎已经忙得晕头转向了.每个人都希望它们做这做那.Windows® Presentation Foundation (WPF) 应用程序中的典型对象会接到各种各样不同的请求:有要求绑定到 ...

随机推荐

  1. Xposed Hook & Anti-hook

    一点简单记录. xposed原理包括将hook的method转为Native.因此可检测如下: for (ApplicationInfo applicationInfo : applicationIn ...

  2. SDM(Supervised Descent Method and its Applications to Face Alignment )

    sdm SDM 人脸对齐的核心内容很简单,就是特征到偏移量的映射:                                           Ix = R I 是特征,x是映射矩阵,R是偏移 ...

  3. Filter过滤器介绍

    简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ht ...

  4. 洛谷P1070 道路游戏(dp+优先队列优化)

    题目链接:传送门 题目大意: 有N条相连的环形道路.在1-M的时间内每条路上都会出现不同数量的金币(j时刻i工厂出现的金币数量为val[i][j]).每条路的起点处都有一个工厂,总共N个. 可以从任意 ...

  5. 《DSP using MATLAB》Problem 6.24

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. LeetCode - Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  7. The Microservices Workflow Automation Cheat Sheet

    Written by Bernd Rücker on Dec 12 2018 in the Best Practices category. Editor’s Note: This post orig ...

  8. encrypt and decrypt data

    https://www.cyberciti.biz/tips/linux-how-to-encrypt-and-decrypt-files-with-a-password.html Encryptin ...

  9. jquery trigger函数和triggerHandler函数的对照

    一句话的差别就是:trigger will bubbling jQuery events (not default DOM events) and triggerHnadler will not do ...

  10. Sql server日期函数用法

    SQL日期函数 SQL日期函数中的类型码可以为0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,20,21,22,23,24,25,100,101,102,103,104,105 ...