这一节试水对象绑定,虽然 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指定项的模板,最后效果如下:
帅得很,继续嗨起来!