Binding的Path(路径)
Binding的源可以是控件(一个控件是另一个控件的Source、控件把自己的容器作为Source),把集合作为ItemsControls的Source,把xml作为Tree或者Menu的Source,或者是不给它Source,让他自己去找。
<TextBox x:Name="textBox" Text="{Binding Value,ElementName=slider1}" BorderBrush="Black" Margin="5"/> <Slider x:Name="slider1" Maximum="100" Minimum="0" Margin="5"/> 这里是通过xaml语言设置的Binding,这里是Binding的标记扩展语法。等价于的C#语句为
Binding的路径支持多级路径,比如上面访问的Path是Value的属性,那么Path下面的次级路径。 Binding的路径还可以通过索引器来作为Path,比如:上面的Path是Value的属性(如果这个Value是一个集合),那么Path也可以是Value[1]来获取集合的第二个对象 Binding的路径还可以通过多级斜线的语法去寻找多级目录下面需要的Path,对应的栗子如下: 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; } } List<Country> countryList=new List<Country>{new Country(){Name="中国"}}; List<string> List=new List<string>(){"Tim","Tom","Blog"}; this.textBox1.SetBinding(TextBox.TextProperty,new Binding("/"){Source=List}); this.textBox2.SetBinding(TextBox.TextProperty,new Binding("/Length") {Source=List,Mode=BindingMode.OneWay}); this.textBox3.SetBinding(TextBox.TextProperty,new Binding("/[2]") {Source=List,Mode=BindingMode.OneWay});
<StackPanel.Resources> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |