在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target)。一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象;这样,数据就会通过Binding送达UI层,被UI层展现。

  首先我们创建一个名为Student的类,这个类的实例作为数据源在UI上显示:  

    public class Student
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
}

  Binding是一种自动机制,当值变化后属性要有能力通知Binding,让Binding把变化传递给UI元素。怎样才能让一个属性具备这种通知Binding值已经变化的能力呢?方法是在属性的set语句中激发一个PropertyChanged事件。这个事件不需要我们自己声明,我们要做的是让作为数据源的类实现System.ComponentModel名称空间中的INotifyPropertyChanged接口。当为Binding设置了数据源后,Binding就会自动侦听来自这个接口的PropertyChanged事件。  

    public class Student:INotifyPropertyChanged
{
private string name;
public string Name
{
set
{
name = value;
NotifyPropertyChanged("Name");
}
get { return name; }
} public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
}

  当Name属性的值发生变化时PropertyChanged事件就会被激发,Binding接收到这个事件后发现事件的消息告诉它是名为Name的属性发生了值得改变,于是就会通知Binding目标端的UI元素显示新的值。

  XAML代码:  

    <Grid>
<TextBox x:Name="Box" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,50,0,0" Height="30" Width="200"/>
<Button Content="按钮" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="120,150,0,0" Height="30" Width="160" Click="Button_Click"/>
</Grid>

  C#代码:  

public partial class MainWindow : Window
{
private Student student;
public MainWindow()
{
InitializeComponent();
student = new Student();
Binding binding = new Binding();
binding.Source = student;
binding.Path = new PropertyPath("Name");
BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding);
} private void Button_Click(object sender, RoutedEventArgs e)
{
student.Name = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
}

  其中:Binding binding=new Binding()声明Binding类型变量并创建实例,然后使用binding.Source=student为Binding实例指定数据源,最后使用binding.Path = new PropertyPath("Name")为Binding指定访问路径。把数据源和目标连接在一起的任务是使用BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding)方法完成的。

  BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding)中的参数介绍:

  第一个参数主要是指定Binding的目标。

  第二个参数是用于为Binding指明将数据绑定到目标的那个属性中去,一般都为目标的依赖属性。

  第三个参数是指定那个Binding将数据源与目标关联起来。

  上面的代码还可以简化如下:

        private void SetBinding()
{
student = new Student();
this.Box.SetBinding(TextBox.TextProperty, new Binding("Name") { Source=student});
}

  Binding模型如下:

  代码下载

深入浅出WPF之Binding的使用(一)的更多相关文章

  1. 深入浅出WPF之Binding的使用(二)

    在上一篇中介绍了Binding的基本绑定方法,这一篇中我们在深入的介绍Binding的其他用法. Binding的源也就是数据的源头,在日常的工作中,除了使用像上一篇中的Student对象作为数据源外 ...

  2. 《深入浅出WPF》重点摘要(—)Binding自动通知机制

    最近因为公司的项目需要用WPF开发,就学习了一下WPF.刚开始只是用到什么就百度什么,虽然功能是实现了,但还是没有弄清楚原理(如果不弄清原理,会感觉很心虚,整个人会没底气),所以决定找个教程系统地学一 ...

  3. [转]深入浅出WPF(7)——数据的绿色通道,Binding

    本文转自:http://liutiemeng.blog.51cto.com/120361/95273 小序: 怎么直接从2蹦到7啦?!啊哦,实在是不好意思,最近实在是太忙了,忙的原因也非常简单——自己 ...

  4. WPF之Binding初探

    初学wpf,经常被Binding搞晕,以下记录写Binding的基础. 首先,盗用张图.这图形象的说明了Binding的机理. 对于Binding,意思是数据绑定,基本用法是: 1.在xmal中使用 ...

  5. 【分享】深入浅出WPF全系列教程及源代码

    来源:http://blog.csdn.net/yishuaijun/article/details/21345893 本来想一篇一篇复制的.但是考虑到别人已经做过了,就没有必要了吧,就给大家一个目录 ...

  6. 深入浅出WPF(1)—转(http://liutiemeng.blog.51cto.com/120361/91631/)

    深入浅出WPF(1)——什么是WPF 2008-05-15 19:06:00   小序:   Hi,大家好!几乎两个月没有写技术文章了.这两个月,我在学习WPF.回顾一下两个月的学习历程,有两个感觉— ...

  7. 《深入浅出WPF》 学习笔记

    <深入浅出WPF> 序言 1. 什么是WPF    2. 为什么要学习WPF 第一章 XAML概览 1. XAML是什么? 2. XAML有哪些优点 第二章 从零起步认识XAML 1. 新 ...

  8. WPF之Binding对数据的转换(第五天)

    Binding在Slider控件与TextBox控件之间建立关联,值可以互相绑定,但是它们的数据类型是不同的,Slider是Double类型,Text为String.原来,Binding有一种机制称为 ...

  9. WPF之Binding的三种简单写法

    环境 类代码 public class Person:INotifyPropertyChanged { private string name; public string Name { get { ...

随机推荐

  1. Linux I/O优化 磁盘读写参数设置

    关于页面缓存的信息,可以用cat /proc/meminfo 看到.其中的Cached 指用于pagecache的内存大小(diskcache-SwapCache).随着写入缓存页,Dirty 的值会 ...

  2. difference between TotalFreeSpace and AvailableFreeSpace

    Refer:http://stackoverflow.com/questions/7275806/what-is-the-difference-between-totalfreespace-and-a ...

  3. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...

  4. 爬豆瓣影评,记下解决maximum recursion depth exceeded in cmp

    #主要是爬取后给别人做自然语言分析,没其他意思. #coding=utf8 import requests,re from lxml import etree import sys reload(sy ...

  5. [Unity3D] 01 - Try Unity3D

    01. Move and Rotate 标准全局坐标系 Keyboard using UnityEngine; using System.Collections; public class NewBe ...

  6. UML类图关系(转,添加了实例)

    UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Associati ...

  7. C mysql (C API Commands out of sync; you can't run this command now)

    错误出现在当一个用户使用查询,另一个用户再使用此sql连接进行查询的时候: 原因是因为上一次使用此sql连接进行查询时没有将所有的结果集给释放掉,在所有使用此sql连接进行查询的地方将所有的结果集给释 ...

  8. 子窗口访问父页面iframe中的iframe,top打开的子窗口访问父页面中的iframe中的iframe

    子窗口访问父页面iframe中的iframe 子窗口访问最顶层页面中的iframe中的iframe top打开的子窗口访问父页面中的iframe中的iframe top打开的子窗口访问最顶层页面中的i ...

  9. docker in centos error

    centos 7 Docker 启动了一个web服务 但是启动时 报 WARNING: IPv4 forwarding is disabled. Networking will not work. 网 ...

  10. IIS URL Rewrite Module的防盗链规则设置

    IIS版本:IIS 7.5 URL Rewrite组件:IIS URL Rewrite Module(http://www.iis.net/downloads/microsoft/url-rewrit ...