WPF之Binding的使用
引出:
在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的使用的更多相关文章
- WPF之Binding深入探讨
原文:http://blog.csdn.net/fwj380891124/article/details/8107646 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在 ...
- WPF数据绑定Binding(二)
WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...
- WPF的Binding功能解析
1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...
- 【转】WPF中Binding的技巧(一)
WPF中Binding的技巧(一) 在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到 ...
- WPF之Binding深入探讨--Darren
1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...
- 深入浅出WPF之Binding的使用(一)
在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象:这 ...
- WPF之Binding【转】
WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢, ...
- WPF之Binding深入探讨 转载:http://blog.csdn.net/fwj380891124/article/details/8107646
1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
随机推荐
- FAL[client]: Failed to request gap sequence GAP - thread 1 sequence 29-29
一:问题描述 主备库所在机器都重启后,rac(主库)开机自动启动,但备库(单实例)不是开机启动,现已手工启动到mount,并已在备库上alter database recover standby d ...
- Android中通过Java获取Webview加载内容
有时候我们需要在加载webview时,获取加载完成的内容,当然,WebView也是有可能包含javascript.通过以下操作,我们是可以获取到WebView加载的内容. 1.自定义一个内部类,获取W ...
- 在thread 引用pthread_t 空指针的问题
错误 pthread_t *thr; pthread_create(thr,NULL,threadhander(),NULL); 正确 pthread_t thr; pthread_create(&a ...
- Oracle数据库之FORALL与BULK COLLECT语句
Oracle数据库之FORALL与BULK COLLECT语句 我们再来看一下PL/SQL块的执行过程:当PL/SQL运行时引擎处理一块代码时,它使用PL/SQL引擎来执行过程化的代码,而将SQL语句 ...
- HTML5 canvas中的路径方法
路径方法 fill() 填充当前绘图(路径) stroke() 绘制已定义的路径 begin ...
- HTML5 canvas 中的线条样式
线条样式属性 lineCap 设置或返回线条的结束端点样式 butt 默认.向线条的每个末端添加平直的边缘. round 向线条的每个末端添加圆形线帽. ...
- javascript中的screen对象
screen对象通常包含下列属性(许多浏览器都加入了自己的属性): 1.availHeight : 窗口可以使用的屏幕的高度(以像素计)其中包括操作系统元素(如window工具栏)需要的空间. 2. ...
- jquery中attr、prop、data
在高版本的jquery中获取标签的属性,可以使用attr().prop().data(),那么这些方法有什么区别呢? 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元 ...
- linux磁盘空间用满的处理方法
linux下空间满可能有两种情况 可以通过命令 df -h 查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) df -i 查看索引节点的占用(Inodes) 磁盘块和索引节点其中之一满 ...
- php 编译安装选项
./configure --prefix=/usr/local/php/ --with-config-file-path=/etc/php5/cli/ --with-config-file-scan- ...