深入浅出WPF之Binding -- 笔记(2015.03.01)

    当使用一个集合或DataView作为Binding的源时,如果想把它的默认元素当作Path使用,则需要使用"/"语法;如果集合元素的属性仍然还是一个集合,又想把子级集合中的元素当作Path,则可以使用多级斜线的语法(即一路"斜线"下去):

事例一:

    XAML:

    <StackPanel>
        <TextBox x:Name="textBox1" BorderBrush="Black" Margin="5"/>
        <TextBox x:Name="textBox2" BorderBrush="Black" Margin="5"/>
        <TextBox x:Name="textBox3" BorderBrush="Black" Margin="5"/>
    </StackPanel>


    C#代码:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<string> stringList = new List<string>() { "Tom", "Tim", "Bruce" };
            this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList });
            this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay });
            this.textBox3.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = stringList, Mode = BindingMode.OneWay });
        }
    }

事例二:

    XAML代码同上;

    C#代码:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitializeList();
        }

        private void InitializeList()
        {
            /*List<Country> countryList = new List<Country>
            {
                new Country
                {
                    Name = "中国",
                    ProvinceList = new List<Province>
                    {
                        new Province
                        {
                            Name = "四川",
                            CityList = new List<City>
                            {
                                new City {Name = "成都"}
                            }
                        }
                    }
                }
            };*/
            City city = new City { Name = "成都" };
            Province province = new Province { Name = "四川", CityList = new List<City> { city } };
            Country country = new Country { Name = "中国", ProvinceList = new List<Province> { province } };
            List<Country> countryList = new List<Country> { country };
            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 });
        }
    }

    Binding源本身就是数据,此时无法通过它的某个属性来访问这个数据,这是只需要将Path的值设置为".",事例如下:

    XAML:

        <StackPanel>
        <StackPanel.Resources>
            <sys:String x:Key="myString">
                菩提本无树,明镜亦非台。
                本来无一物,何处惹尘埃。
            </sys:String>
        </StackPanel.Resources>
        <TextBlock x:Name="textBlock1" TextWrapping="Wrap" Text="{Binding [[Path=].,] Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5"/>
    </StackPanel> 注:[]中的内容可省略。


你可能感兴趣的:(WPF,binding)