WPF真入门教程19--对象数据绑定

这一节试水对象绑定,虽然 XmlDataProvider 对 XML 非常有用,但是当您想绑定到对象或对象列表时,可以创建 ObjectDataProvider 作为资源。ObjectDataProvider 的 ObjectType 指定将提供数据绑定源的对象,而 MethodName 则指示为获得数据而调用的方法。在项目中添加一个类StudentService,该类通过GetStudentList的方法来返回Student列表

 然后添加布局控件等,具体代码如下(接上一节代码):

 <Window x:Class="WpfApp6.BindWindow"
        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:WpfApp6"
        mc:Ignorable="d"
        Title="BindWindow" Height="765" Width="980">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="250"/>
            <RowDefinition Height="200"/>
            <RowDefinition Height="138*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Row="0" Grid.Column="0">
            <TextBlock Width="248" Height="24" Text="简单绑定,股票名称:"   TextWrapping="Wrap" FontSize="15"/>
            <ListBox Name="mylista" Width="348" Height="150"  FontSize="15" Margin="0,0">
                <ListBoxItem Content="全通教育"/>
                <ListBoxItem Content="京大智慧"/>
                <ListBoxItem Content="宝钢股份"/>
                <ListBoxItem Content="浦发银行"/>
                <ListBoxItem Content="工商银行"/>
                <ListBoxItem Content="中国建筑"/>
                <ListBoxItem Content="长城汽车"/>
                <ListBoxItem Content="山东电子"/>
                <ListBoxItem Content="浙江吉利"/>
            </ListBox>
            <TextBlock Width="248" Height="24" Text="你选中的股票:" Margin="0,5,110,0"  FontSize="15"/>
            <!--ElementName 属性指示要绑定的控件名称。Path 属性指示绑定到ListBox元素的属性-->
            <TextBlock Width="248" Height="24" Text="{Binding ElementName=mylista,Path=SelectedItem.Content}"  FontSize="15" Margin="0,0,110,0"/>
        </StackPanel>
        <StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Height="240" Margin="10,10,0,0" VerticalAlignment="Top" Width="466">
            <Label Content="绑定多个控件及单向双向绑定和更新触发器" FontSize="15" />
            <ListBox Height="73" Margin="40,0,60,0" FontSize="15" Name="mylistb">
                <ListBoxItem Content="green"/>
                <ListBoxItem Content="blue"/>
                <ListBoxItem Content="black"/>
                <ListBoxItem Content="yellow"/>
                <ListBoxItem Content="orange"/>
            </ListBox>
            <Label Content="背景色" Height="23"  FontSize="12" Margin="0,2,366,0"/>
            <!--TwoWay双向绑定,OneWay单向绑定-->
            <!--UpdateSourceTrigger意指更新触发器,表示更新数据源方式:Explicit、LostFocus(默认) 和 PropertyChanged
            1:Explicit,则不会更新源,除非从代码中调用 BindingExpression.UpdateSource
            2:LostFocus ,默认值,指示该控件失去焦点时才会更新数据源。
            3:PropertyChanged,控制属性每次发生更改时就立即更新数据源。-->
            <TextBox   FontSize="15"   Text="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=TwoWay,UpdateSourceTrigger=LostFocus}"  Background="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=OneWay}" Margin="0,0,306,0"/>
            <TextBox FontSize="15"   Text="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="0,10,306,0"/>
            <TextBlock TextWrapping="Wrap"  Text="" Height="39" Margin="0,10,0,0" Background="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=OneWay}"/>
        </StackPanel>
        <!--XML数据绑定-->
        <StackPanel HorizontalAlignment="Left" Height="180" Margin="10,10,0,0" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Width="476">
            <!--Resources属性指定外部资源为XmlDataProvider-->
            <StackPanel.Resources>
                <!--Source 属性指定资源文件名,XPath 属性指定路径名-->
                <XmlDataProvider x:Key="MyColors"  Source="Colors.xml"  XPath="colors"/>
            </StackPanel.Resources>
            <Label Content="XML数据绑定"/>
            <ListBox Height="100" Margin="0,0,10,0" Name="mylistc" ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}"/>
            <Label Content="选中的颜色"/>
            <TextBlock TextWrapping="Wrap" Background="YellowGreen" Text="{Binding ElementName=mylistc,  Path=SelectedValue, Mode=OneWay}"/>
        </StackPanel>
        <!--对象绑定-->
        <StackPanel Grid.Row="1"  Grid.Column="1" HorizontalAlignment="Left" Height="180" Margin="10,10,0,0"  VerticalAlignment="Top" Width="466">
            <StackPanel.Resources>
                <!--ObjectType 指定将提供数据绑定源的对象,而 MethodName 则指示为获得数据而调用的方法-->
                <ObjectDataProvider x:Key="students"  ObjectType="{x:Type local:StudentService}"  MethodName="GetStudentList"/>
                <!--DataTemplate数据模板,允许定义自己的显示样式-->
                <DataTemplate x:Key="studentLayout" DataType="students">
                    <StackPanel Orientation="Horizontal" Height="28" Width="244">
                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"/>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Birthday}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Age}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Country}" FontWeight="Bold" Foreground="red"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </StackPanel.Resources>
            <Label Content="对象绑定"  HorizontalAlignment="Left" Margin="16,4,0,0"  VerticalAlignment="Top"/>
            <!--ItemTemplate指定显示样式 ,ItemsSource指定显示来源,IsSynchronizedWithCurrentItem表示异步检索数据-->
            <ListBox  Name="mylistd" Width="450" Height="147" IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding Source={StaticResource students}}"  ItemTemplate="{DynamicResource studentLayout}" Margin="8,0">
            </ListBox>
        </StackPanel>

    </Grid>
</Window>
 

 本示例中,StackPanel.Resources指定控件的数据源来自本地命名空间中的StudentService类下的GetStudentList方法,DataTemplate 定义成如何显示Student信息的布局样式,ItemsSource 属性指定ListBox项的来源,ItemTemplate指定项的模板,最后效果如下:

 帅得很,继续嗨起来!

wpf经典教程 共83页 WPF体系结构........... 3 WPF应用程序管理... 5 一、WPF应用程序由System.Windows.Application类进行管理.................... 5 二、创建WPF应用程序......................................................... 5 三、应用程序关闭................................................................... 6 四、Application对象的事件.................................................... 7 五、WPF应用程序生存周期................................................... 9 WPF窗体................. 10 一、窗体类...... 10 1、XAML文件............................................................... 10 2、后台代码文件........................................................... 10 二、窗体的生存周期..............................................11 1、显示窗体.......................................................11 2、关闭窗体......................................................12 3、窗体的激活................................................... 12 4、窗体的生存周期............................................... 12 三、其他窗体相关的属性、方法、事件.............................. 15 四、定义异形窗体................................................ 16 StackPanel、WrapPanel、DockPanel 容器............................ 19 一、StackPanel 19 1、可以使用Orientation属性更改堆叠的顺序............ 19 2、设置控件的属性,调整控件的显示......................... 20 二、WrapPanel. 20 三、DockPanel. 21 Grid UniformGrid容器......................................... 22 一、Grid........... 22 二、使用GridSplit分割........................................................ 23 三、UniformGrid ............................................. 25 Canvas、InkCanvas布局................................ 27 一、Canvas ...... 27 二、InkCanvas . 27 WPF对控件其类型的继承方式如下............................................. 29 WPF控件内容模型. 32 一、ContentControl模型....................................... 35 二、HeaderedContentControl模型......................................... 36 三、ItemsControl模型....................................... 38 1、使用ItemSource属性................................................ 38 2、使用Items属性........................................................ 40 四、HeaderedItemsControl模型............................................. 42 Panel Decorator TextBlock内容模型............................ 44 一、Panel内容模型............................................ 44 二、Decorator内容模型........................................ 45 三、TextBlock模型........................................ 46 四、TextBox模型........................................... 49 依赖项属性和路由事件......................................... 50 一、依赖项属性(Dependency Property................................ 50 1、依赖项属性与CLR 包装属性.................................. 50 2、使用由依赖项属性提供的属性功能......................... 51 3、自定义依赖项属性及重写依赖项属性..................... 52 二、路由事件(RoutedEvent)............................................. 53 键盘输入、鼠标输入、焦点处理................................ 56 一、键盘类和键盘事件..................................... 56 二、鼠标类和鼠标事件...................................... 57 三、焦点处理.. 60 1、键盘焦点: ....................................... 60 2、逻辑焦点........................................... 61 3、键盘导航.............................................. 61 4、焦点事件.................................... 61 WPF命令................. 63 一、命令: ...... 64 二、命令源...... 65 三、命令目标.. 66 四、命令绑定.. 67 WPF资源................. 70 一、什么是资源............................................. 70 二、资源的定义及XAML中引用......................................... 70 三、XAML解析资源的顺序.................................................. 74 四、静态资源(StaticResource)和动态资源(DynamicResource) .............. 77 五、不同类型的资源............................................................. 81 1、程序集资源。........................................................... 81 2、对象资源................................................................... 82
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqwest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值