WPF新手之将对象绑定到非Items控件
2010-09-23 20:48
381 查看
<StackPanel> <StackPanel.Resources> <Style TargetType="{x:Type Label}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Label}"> <StackPanel> <TextBlock Text="{Binding Path=Name}"/> <TextBlock Text="{Binding Path=Degree}"/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </StackPanel.Resources> <Label x:Name="MyGroup" DataContext="{Binding Mode=OneWay}"/> </StackPanel>
注意这里是使用ControlTemplate,不像Items控件使用的是ItemTemplate。
此方法适用于所有从ContentControl继承的控件。
注意①由于C#中的string实际上是只读的,所以将TextBox用TwoWay绑定到string会抛出异常。这时要用对象将string包装其中。②非Items控件的对象源是单个对象;而Items控件的对象源必须是个Collection,即使是单个对象也要添加到Collection中作为对象源。
或者:
<DockPanel x:Name="dockPanel"> <TextBlock DockPanel.Dock="Top"> <TextBlock>firstName:</TextBlock> <TextBox Text="{Binding Path=FirstName}" Width="100"></TextBox> <TextBlock>lastName:</TextBlock> <TextBox Text="{Binding Path=LastName}" Width="100"></TextBox> </TextBlock> <Button x:Name="btnView" DockPanel.Dock="Bottom" Height="30">view</Button> </DockPanel>
相关文章推荐
- WPF 实现数据结构对象与UI控件的绑定
- WPF新手之控件多对一的绑定
- WPF新手之将如何将一个成员变量或自定义类绑定到控件
- 绑定用户控件赋值对象(非基本数据类型)
- WPF学习(1):控件之间的数据绑定
- 使用反映实现前端WEB窗口控件与后台数据库类对象的动态绑定
- WPF 数据控件绑定数据与 ASP.NET数据控件绑定数据的区别
- 关于wpf的命令绑定command的指向控件绑定问题,Path=和ElementName=设定
- 面向对象开发中的数据控件绑定方案
- WPF 控件之ComboBox绑定
- WPF绑定.NET对象(二)Visifire的数据绑定
- 使用反射将业务对象绑定到 ASP.NET 窗体控件2
- wpf GridView 动态添加 GridViewColumn 及绑定控件
- 获取控件行或控件项对象绑定到的基础数据
- WPF——数据绑定(二)绑定方法—绑定本地对象
- InteropBitmap指定内存,绑定WPF的Imag控件时刷新问题。
- WPF 将控件绑定到变量
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- DetailsView控件绑定对象列表时出现某属性名没有找到的错误
- WPF绑定不到父控件属性