WPF TextBox 绑定List集合

在学习深入浅出WPF第六章节Binding,敲写TextBox绑定List列表案例时,怎么也不能达到书上的效果,后来发现,书上的代码案例给错了。哎,这回记忆深刻了。特此写博客记录下。好了上代码。

xmal代码如下:

<Window x:Class="ListTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid>
            <StackPanel Margin="10,10,12,12" Name="stackPanel1">
                <TextBox Height="23" Margin="10" Name="textBox1" Width="120" />
                <TextBox Height="23" Margin="10" Name="textBox2" Width="120" />
                <TextBox Height="23" Margin="10" Name="textBox3" Width="120" />
            </StackPanel>
        </Grid>
    </Grid>
</Window>

.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Country> countryList = new List<Country>
            { new Country()
            {
                Name = "中国",
                ProvinceList = new List<Province>()
                { new Province()
                    {
                        Name = "四川",
                        CityList = new List<City>()
                        { new City()
                            { Name = "成都" }
                        }
                    }
                }
            }
            };
            this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = countryList });
            this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/Name") { Source = countryList });
            this.textBox3.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/CityList/Name") { Source = countryList });
            Console.WriteLine(countryList[0].Name);
            Console.WriteLine(countryList[0].ProvinceList[0].Name);
            Console.WriteLine(countryList[0].ProvinceList[0].CityList[0].Name);
        }
    }
    class City
    {
        public string Name { get; set; }
    }
    class Province
    {
        public string Name { get; set; }
        public List<City> CityList { get; set; }
    }
    class Country
    {
        public string Name { get; set; }
        public List<Province> ProvinceList { get; set; }
    }

   
}

运行结果:

目的是textBox1绑定countryList[0].Name。

textBox2绑定countryList[0].ProvinceList[0].Name。

textBox3绑定countryList[0].ProvinceList[0].CityList[0].Name。

实际上“/”就是获取下一子集合。

这是正确的结果。书上错误的案例是这么写的:

            this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = countryList });
            this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList.Name") { Source = countryList });
            this.textBox3.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/CityList.Name") { Source = countryList });

把/ProvinceList.Name 换成/ProvinceList/Name就可以了。

案例出自深入浅出WPF第91页。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页