1. 程式人生 > >WPF Binding學習(三)

WPF Binding學習(三)

圖片 mod this color get 集合類 com src 技術分享

轉自;http://blog.csdn.net/lisenyang/article/details/18312199

1.控件與控件間的雙向綁定

WPF還支持控件作為數據源,

 <TextBox Name="txt_Source" Width="120" HorizontalAlignment="Left"></TextBox>
 <TextBox Text="{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做為數據源時,如果我們想把它默認的元素做為數據源使用,則需要使用下面的語法:

  List<string> list = new List<string>() { "狗娃","狗剩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 });

效果如下

技術分享圖片

如果想要綁定綁定第二個元素,則直接使用"點"即可

  List<string> list = new List<string>() { "狗娃","狗剩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 IList<City> Citys { get; set; }
    }
    public class City
    {
        public string Name { get; set; }
        public IList<District> Districts { get; set; }
    }
    public class District
    {
        public string Name { get; set; }
    }

然後我們使用多級斜線進行綁定

  List<Province> list = new List<Province>()
            {
                new Province
                {
                    Name ="河北",
                    Citys = new List<City>
                    {
                        new City
                        {
                            Name = "邯鄲",
                            Districts = new List<District>{ 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 });

WPF Binding學習(三)