引出:

在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的源也就是数据的源头,在日常的工作中,除了使用像上一篇中的Student对象作为数据源外,我们还有更多的选择,比如控件把自己或者自己的容器或子级元素当做源,用一个控件作为另一个控件的数据源等等。

   把控件作为Binding源。  

    <Grid>
<TextBox x:Name="Box" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,50,0,0" Height="30" Width="200" Text="{Binding Path=Value,ElementName=Silder1,Mode=TwoWay}"/>
<Slider x:Name="Silder1" HorizontalAlignment="Left" Margin="100,120,0,0" Maximum="100" Minimum="0" VerticalAlignment="Top" Width="200"/>
</Grid>

  其中Text="{Binding Path=Value,ElementName=Silder1,Mode=TwoWay}"等价于C#代码:

  this.Box.SetBinding(TextBox.TextProperty, new Binding("Value") { Source=Silder1});

  或 Binding binding = new Binding() { Path = new PropertyPath("Value"), Source = Silder1 };

    this.Box.SetBinding(TextBox.TextProperty,binding);

  

深入:

  Binding还支持多级路径(通俗地将就是一路“点”下去)。比如,如果想让一个TextBox显示另外一个TextBox的文本长度,我们可以这么写:

    <Grid>
<TextBox x:Name="Box" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,50,0,0" Height="30" Width="200" />
<TextBox x:Name="Box2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,123,0,0" Height="30" Width="200" Text="{Binding Path=Text.Length,ElementName= Box,Mode=OneWay}" />
</Grid>

等于C#代码如下:  

  Binding binding = new Binding("Text.Length") { Source=Box,Mode=BindingMode.OneWay};   this.Box2.SetBinding(TextBox.TextProperty,binding);

WPF之Binding的使用的更多相关文章

  1. WPF之Binding深入探讨

    原文:http://blog.csdn.net/fwj380891124/article/details/8107646 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在 ...

  2. WPF数据绑定Binding(二)

    WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...

  3. WPF的Binding功能解析

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  4. 【转】WPF中Binding的技巧(一)

    WPF中Binding的技巧(一)   在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到 ...

  5. WPF之Binding深入探讨--Darren

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  6. 深入浅出WPF之Binding的使用(一)

    在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象:这 ...

  7. WPF之Binding【转】

    WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢, ...

  8. WPF之Binding深入探讨 转载:http://blog.csdn.net/fwj380891124/article/details/8107646

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  9. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

随机推荐

  1. solr单机版的搭建

    一.solr单机版的搭建 1.运行环境 solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(ja),本教va写的Servlet容器程 ...

  2. 关于iOS应用管理之九宫格的坐标计算以及与UIScrollView的结合

    关于九宫格的布局以及坐标的计算,对于大多数的iOS初学者甚至有一定能力的学者来说都是一大难题,在此写者通过自己的开发经验以及多次应用,把自己的所学所得分享给大家,就通过应用管理来进行浅谈一二.     ...

  3. iOS8中添加的extensions总结(四)——Action扩展

    Action扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutorials> 1.准备 本次教程利用网站bitly.com进行 bit ...

  4. Jquery中dialog属性小记

    代码如下: $('#dialogDiv').dialog( { hide:true, //点击关闭是隐藏,如果不加这项,关闭弹窗后再点就会出错. autoOpen:false, height:380, ...

  5. restful风格的webservice开发之概念准备篇

    理解restful:什么是rest,rest是Representational State Transfer的缩写,翻译过来就是“表现层状态转化”.这里有点不准确,其实rest省略了主语表现层指的是资 ...

  6. js 懒加载

    需要的js <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> < ...

  7. js console.log 打印 对像 数组 详解

    console.log是什么东西,其实就是一个打印js数组和对像的函数而已,就像是php的print_r,var_dump.console.log这个函数本身没什么好说的,这篇博客告诉大家怎么去用这个 ...

  8. Asp.net Core 缓存 MemoryCache 和 Redis

    Asp.net Core 缓存 MemoryCache 和 Redis 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitH ...

  9. OC 代码规范

    我们写出来的代码会给很多人看,为了使代码清晰简洁,方便阅读理解,都会统一遵从一定的代码规范,Objective-C同样如此.   主要参考规范:   1. Google Objective-C Sty ...

  10. git操作的各种命令整理

    1.常用的Git命令   命令 简要说明 git add 添加至暂存区 git add–interactive 交互式添加 git apply 应用补丁 git am 应用邮件格式补丁 git ann ...