WPF深入浅出的 6.3.3 Binding的路径(Path)

根据WPF的6.3.3 Binding的路径(Path)

代码验证结果

 

  public class City
    {
        public string Name { get; set; }
    }
    public class Province
    {
        public string Name { get; set; }
        public List<City> CityList { get; set; }
    }
    public class Country
    {
        public string Name { get; set; }
        public List<Province> ProvinceList { get; set; }
    }

  

  Country country = new Country()
            {
                Name = "中国",
                ProvinceList = new List<Province>()
                {
                    new Province(){
                        Name="四川",
                         CityList=new List<City>(){
                             new City(){Name="攀枝花"}
                         }
                    }
                }
            };
            //country.ProvinceList[0].Name
            Binding binding = new Binding("Name");
            binding.Source = country;
            textBox1.SetBinding(TextBox.TextProperty, binding);

            Binding binding1 = new Binding("ProvinceList/Name");
            binding1.Source = country;
            textBox2.SetBinding(TextBox.TextProperty, binding1);

            Binding binding2 = new Binding("ProvinceList/CityList/Name");
            binding2.Source = country;
            textBox3.SetBinding(TextBox.TextProperty, binding2);

和书上所说有点区别,暂时不知道为何如此?

书上写的是/Name, /ProvinceList.Name和/ProvinceList/CityList.Name

 

posted on 2024-04-13 15:25  梅卫军  阅读(149)  评论(0编辑  收藏  举报