日期:2014-05-18  浏览次数:20978 次

Binding的path读取对象的子集合赋值
C# code

  List<County> c_list = new List<County>();
            City city = new City();
            city.Name = "City";
            Province p = new Province();
            p.Name = "Province";
            p.CityList = new List<City>() { city};
            County c = new County();
            c.Name = "County";
            c.ProvinceList = new List<Province>() { p};
            c_list.Add(c);

            this.txt_city.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source=c_list});
            this.txt_province.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList.Name") { Source = c_list });
            this.txt_county.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/CityList.Name") { Source = c_list });


C# code

<StackPanel>
                <TextBox x:Name="txt_city" Margin="5" BorderBrush="Red"/>
                <TextBox x:Name="txt_province" Margin="5" BorderBrush="Red"/>
                <TextBox x:Name="txt_county" Margin="5" BorderBrush="Red"/>
            </StackPanel>




求答案,为什么后两个文本框没有显示内容,只有第一个txt_city显示了一个County。在看wpf,基础啊,对这书本抄的都没写出来

------解决方案--------------------
C# code

this.txt_city.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = c_list });
this.txt_province.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList[0].Name") { Source = c_list });
this.txt_county.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList[0].CityList[0].Name") { Source = c_list });