WPF Binding学习(三)
2021-02-19 11:17
阅读:791
转自;http://blog.csdn.net/lisenyang/article/details/18312199
1.控件与控件间的双向绑定
WPF还支持控件作为数据源,
"txt_Source" Width="120" HorizontalAlignment="Left"> "{Binding ElementName=txt_Source,Path=Text}" Name="txt_Target" Width="120" HorizontalAlignment="Left">
上面代码就可以实现控件之间的双向绑定,我们发现我们的绑定数据源不是Source,而是ElementName.
当然我们也可以使用代码来实现
Binding binding = new Binding("Text") { Source = this.txt_Source }; this.txt_Target.SetBinding(TextBox.TextProperty, binding);
使用代码绑定我们依然使用的Source来绑定数据源
2.多级路径
WPF支持多级路径,也就是我们通俗的说“点”下去。
我们改下上面的代码
Binding binding = new Binding("Text.Length") { Source =this.txt_Source,Mode = BindingMode.OneWay }; this.txt_Target.SetBinding(TextBox.TextProperty, binding);
注意:因为源数据和目标数据无法做到匹配,所以无法双向绑定。
我们可以看到我们绑定的数据是Text属性中的Length属性,这就是我们所谓的多级路径.
3.索引绑定
我们知道,集合类型是索引器(Indexer)又称为带参属性。既然是属性,索引器也能作为Path来使用,比如我们想让一个TextBox显示另外一个TextBox的第1个字符
Binding binding = new Binding("Text[1]") { Source =this.txt_Source,Mode = BindingMode.OneWay }; this.txt_Target.SetBinding(TextBox.TextProperty, binding);
4.集合绑定
当使用一个集合或者DataView做为数据源时,如果我们想把它默认的元素做为数据源使用,则需要使用下面的语法:
Liststring> list = new Liststring>() { "狗娃","狗剩2"}; txt_List1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = list, Mode = BindingMode.OneTime }); txt_List2.SetBinding(TextBox.TextProperty, new Binding("/[1]") { Source = list,Mode = BindingMode.OneTime }); txt_List3.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = list, Mode = BindingMode.OneTime });
效果如下
如果想要绑定绑定第二个元素,则直接使用"点"即可
Liststring> list = new Liststring>() { "狗娃","狗剩2"}; txt_List1.SetBinding(TextBox.TextProperty, new Binding("[1]") { Source = list, Mode = BindingMode.OneTime }); txt_List2.SetBinding(TextBox.TextProperty, new Binding("[1].[1]") { Source = list,Mode = BindingMode.OneTime }); txt_List3.SetBinding(TextBox.TextProperty, new Binding("[1].Length") { Source = list, Mode = BindingMode.OneTime });
如果想把子集集合中元素作为Path,可以使用多级斜线方法(一路"斜线"下去);
我们创建一个省市区关系的类型
public class Province { public string Name { get; set; } public IListCitys { get; set; } } public class City { public string Name { get; set; } public IList Districts { get; set; } } public class District { public string Name { get; set; } }
然后我们使用多级斜线进行绑定
Listlist = new List () { new Province { Name ="河北", Citys = new List { new City { Name = "邯郸", Districts = new List { new District { Name="邯山区"} } } } } }; txt_List1.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = list, Mode = BindingMode.OneTime }); txt_List2.SetBinding(TextBox.TextProperty, new Binding("/Citys/Name") { Source = list, Mode = BindingMode.OneTime }); txt_List3.SetBinding(TextBox.TextProperty, new Binding("/Citys/Districts/Name") { Source = list, Mode = BindingMode.OneTime });
当然也可以使用多级"点"进行绑定
txt_List1.SetBinding(TextBox.TextProperty, new Binding("[0].Name") { Source = list, Mode = BindingMode.OneTime }); txt_List2.SetBinding(TextBox.TextProperty, new Binding("[0].Citys[0].Name") { Source = list, Mode = BindingMode.OneTime }); txt_List3.SetBinding(TextBox.TextProperty, new Binding("[0].Citys[0].Districts[0].Name") { Source = list, Mode = BindingMode.OneTime });
评论
亲,登录后才可以留言!