WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“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; }
}



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

  1. WPF整理-XAML构建后台类对象

    1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...

  2. 福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型

    我们知道,KVC+Runtime可以做非常多的事情.有了这个,我们可以实现很多的效果. 这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量.属性.方法及协议: 并利用它来实现字典转模型 ...

  3. Extjs在form展示后台单个对象的属性

    目的:写一个按钮事件,点击时弹出一个win窗体,里面镶嵌form表单,并且展示后台单个对象的属性 先来后台: public void find(){ String clientId = request ...

  4. 全面了解python中的类,对象,方法,属性

    全面了解python中的类,对象,方法,属性 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...

  5. 028. asp.net数据绑定控件值DataList控件

    DataList控件可以使用模板与定义样式来显示数据并进行数据的选择, 删除及编辑工作. DataList控件的最大特点是一定要通过模板来定义数据的显示格式. 如果要设计出美观的界面, 就需要花费一番 ...

  6. 【WPF】WPF通过RelativeSource绑定父控件的属性

    1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...

  7. React-Native的基本控件属性方法,对React-Native的学习,从熟悉基本控件开始。

    对React-Native的学习,从熟悉基本控件开始. View 属性方法 序号 名称 属性Or方法 类型 说明 1 accessibilityLabel 属性 string   2 accessib ...

  8. python 学习笔记7(类/对象的属性;特性,__getattr__)

    27. 属性的__dict__系统 1)对象的属性可能来自: 其类的定义,叫做类属性 继承父类的定义 该对象实例定义(初始化对象时赋值),叫做对象属性 2)对象的属性存储在对象的 __dict__ 属 ...

  9. ASP.NET后台取html控件值方式

    1.Request.Form[“cbName”]: 可以在后台取到所有为name 为的控件的value值 2.可以通过 把html控件的值付给HiddenField,然后后台调用 3.就是自定义属性 ...

随机推荐

  1. 格而知之15:我所理解的Block(1)

    1.Block 本质上是一个struct结构体,在这个结构体中,最重要的成员是一个函数(当然除函数外还有其他重要的成员). 2.在开始解析Block之前,首先来回顾一下Block的格式.Block相关 ...

  2. .NET 中使用 HttpWebResponse 时 Cookie 的读取

    今天把一个网站登录配置到以前写的蜘蛛程序中,发现不能成功登录.检查后才发现,那个网站在登录成功后,输出了一个特殊路径的 Cookie,由于是使用 HttpWebRequest.Cookies 来获取的 ...

  3. 在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置

    在Maven的settings.xml,可以设置Maven的私有仓库的地址,还可以设置所下载jar包在自己电脑的保存地址(默认不设置保存在个人文件夹的.m2文件夹下). 1.设置私有仓库地址: < ...

  4. MySql命令——命令行客户机的分隔符

    delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...

  5. JavaScript ----------------- 原型式继承

    思想:借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型.为了达到这个目的,看看下面的实现方式 function object(o){ function F(){ } F.protot ...

  6. 代码先行-log4Net初体验

    1.安装 从http://logging.apache.org/log4net/download_log4net.cgi下载编译好的log4Net包并解压. 找到 bin\net\4.0\releas ...

  7. feel倍儿爽

    今天装的360,在卸载就要权限,在自己的电脑卸载360还要权限,真是一物降一物,安装了个牛逼的卸载软件就卸载了

  8. SQL Server -SET ANSI_NULLS

    当ANSI_NULLS 为ON时,遵循SQL92的标准,只能使用IS NULL 来判断值是否为NULL, 而不能使用=或<>来与NULL做比较,任何值包括NULL值与NULL值做=或< ...

  9. Oracle自治事务

    定        义: Autonomous transactions are independent transactions that can be called from within anot ...

  10. UIView的一些常用属性和方法

    UIView的一些常用属性和方法 1. UIView的属性 UIView继承自UIResponder,拥有touches方法. - (instancetype)initWithFrame:(CGRec ...