WPF中的数据绑定

WPF中的数据绑定

基础概念

System.Windows.Data.Binding,他会把两个对象(UI对象与UI对象之间,UI对象与.NET数据对象之间)按照指定的方式粘合在一起,并在他们之间建立一条通信通道。

绑定建立好后,接下来的生命周期中它可以自己独立完成所有的同步工作。

绑定的几个部分:①对象间的绑定 ②绑定到集合 ③数据模板 ④向绑定添加规则和转换器

对象间的绑定

XAML绑定语法

Binding关键字指定(Source)ElementName和源属性(Source Property)

Text=”{Binding ElementName=SourceObjectName,Path=SourceProperty}”

Coding(C#)添加Bindind

1.调用FrameworkElement/FrameworkContentElement对象的SetBinding方法

2.调用BindingOperations.SetBinding静态方法

 1 Binding binding = new Binding();
 2 
 3 //设置源对象
 4 
 5 binding.Source = txtName;
 6 
 7 //设置源属性
 8 
 9 binding.Path = new PropertyPath(“Text”);
10 
11 //添加到目标属性
12 
13 This.tbShowMessage.SetBinding(TextBlock.TextProperty,binding);

Coding(C#)移除Binding

1.BindingOperations.ClearBinding(ClearAllBindings)

2.简单的将目标属性设置为一个新的值,如:currentTextBlock.Text=”it’s a new value”;

绑定对象属性

Path:属性 ElementName必须为DataContextRelativeSourceSource中的一个。

参考

http://www.cnblogs.com/zlgcool/archive/2008/10/22/1316605.html

猜你喜欢

转载自www.cnblogs.com/huangsitao/p/10299842.html