WPF绑定各种数据源之元素控件属性
2015-05-26 11:00
399 查看
一、WPF绑定各种数据源索引
WPF绑定各种数据源之Datatable
WPF绑定各种数据源之object数据源
WPF绑定各种数据源之xml数据源
WPF绑定各种数据源之元素控件属性
WPF绑定基础
二、WPF绑定各种数据源之元素控件属性
1.绑定Value路径
与上面等效的C#代码如下:
效果图:
2、绑定到Text.Length路径
XAML:
与上面等效的C#代码如下:
3、绑定到索引器
此处等效的C#代码略
4、如果Binding的源是集合时,使用默认元素当Path使用,则语法如下
效果图如下:
5.没有Path的绑定
这是一种特殊的情况,Binding源本身就是数据且不需要Path来指明,string、int等基本类型就是这样,他们本身就是数据,无法指出通过那个属性访问这个数据,这时我们只需将Path设置成"."就可以了。请看下面的代码:
XAML:
下面的代码可以写成Text="{BindingPath=.,Source={StaticResourceResourceKey=myStr},Mode=OneWay}"或Text="{BindingSource={StaticResourceResourceKey=myStr},Mode=OneWay}"
二、WPF绑定各种数据源之元素控件属性
1.绑定Value路径
<TextBlockMargin= "327,378,331,363" Name= "textBlock1" Text= "{BindingElementName=slider1,Path=Value}" /> <SliderHeight= "22" Minimum= "0" Maximum= "100" HorizontalAlignment= "Left" Margin= "135,346,0,0" Name= "slider1" VerticalAlignment= "Top" Width= "100" /> |
textBlock1.SetBinding(TextBlock.TextProperty, new Binding(){Path= new PropertyPath( "Value" ),Source=slider1}); |
2、绑定到Text.Length路径
XAML:
<TextBoxHeight= "23" HorizontalAlignment= "Left" Margin= "37,48,0,0" Name= "textBox1" Text= "{Binding VerticalAlignment= "Top" Width= "120" /> <TextBoxHeight= "23" HorizontalAlignment= "Left" Margin= "37,92,0,0" Name= "textBox2" VerticalAlignment= "Top" Width= "120" /> |
textBox1.SetBinding(TextBox.TextProperty, new Binding(){Path= new PropertyPath( "Text.Length" ),Source |
<TextBoxHeight= "23" HorizontalAlignment= "Left" Margin= "37,48,0,0" Name= "textBox1" Text= "{Binding VerticalAlignment= "Top" Width= "120" /> <TextBoxHeight= "23" HorizontalAlignment= "Left" Text= "WorkHard" Margin= "37,92,0,0" Name= "textBox2" VerticalAlignment= "Top" Width= "120" /> |
4、如果Binding的源是集合时,使用默认元素当Path使用,则语法如下
List< string >strCityList= new List< string >(){ "Hangzhou" , "Shanghai" , "Beijing" }; textBox3.SetBinding(TextBox.TextProperty, new Binding( "/" ){Source=strCityList}); textBox4.SetBinding(TextBox.TextProperty, new Binding( "/Length" ){Source=strCityList,Mode=BindingMode.OneWay}); textBox5.SetBinding(TextBox.TextProperty, new Binding( "/[3]" ){Source=strCityList,Mode=BindingMode.OneWay}); |
5.没有Path的绑定
这是一种特殊的情况,Binding源本身就是数据且不需要Path来指明,string、int等基本类型就是这样,他们本身就是数据,无法指出通过那个属性访问这个数据,这时我们只需将Path设置成"."就可以了。请看下面的代码:
<Window.Resources> <sys:Stringx:Key= "myStr" > WorkHandWorkSmart </sys:String> </Window.Resources> |
<TextBoxHeight= "23" HorizontalAlignment= "Right" Margin= "0,65,199,0" Text= "{Binding Name= "textBox6" VerticalAlignment= "Top" Width= "141" /> |
相关文章推荐
- WPF绑定各种数据源之元素控件属性
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF 绑定一(数据源为控件)
- WPF自定义控件,将该控件中包含的控件的某个属性绑定到自定义的属性对外暴露
- WPF 元素tag属性绑定一个属性或一个对象
- WPF 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- WPF 如何在后台进行控件属性的绑定MVVM
- WPF Grid 数据绑定,当数据源发生变化后控件值随之更新
- ASP.Net控件模板中根据绑定对象的属性控制元素隐现技巧
- WPF绑定各种数据源之xml数据源
- WPF 绑定各种数据源之Datatable
- WPF 绑定各种数据源之LINQ检索结果
- wpf 控件的依赖属性和数据绑定
- 【WPF】WPF通过RelativeSource绑定父控件的属性
- 【WPF】对Frame控件的Content属性做绑定时出现的一个小问题
- WPF绑定各种数据源之object数据源
- WPF 数据绑定 1_1 基础知识&绑定到元素属性
- ASP.Net控件模板中根据绑定对象的属性控制元素隐现技巧