Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合的绑定
在本文中将以ListBox为例讲述在Silverlight中Binding数据集合.
在这里我们将实体集的绑定分为三类:
一、直接控件绑定。
二、DataTemplate模板绑定。
三、详细信息绑定。
首先:我们来看第一类直接控件绑定是对控件的ItemsSource属性进行绑定,然后使用SelectedValuePath指定选择值,DisplayMemberPath指定显示值的方式。Xaml代码如下:
<!--第一种:直接绑定一个Collection实体集合-->
<ListBox Height="239" HorizontalAlignment="Left" Margin="112,25,0,0"
Name="lbCollection" VerticalAlignment="Top" Width="198"
ItemsSource="{Binding}" SelectedValuePath="Author" DisplayMemberPath="Name" />
其次:DataTemplate是对象制作一个数据模板,所以的数据实体都安装这个数据模板来呈现,现在我们看看其Xaml代码如下:
<!--第二种:使用DataTemplate绑定一个Collection实体集合-->
<ListBox Height="239" HorizontalAlignment="Left" Margin="478,25,0,0"
Name="lbTemplate" ItemsSource="{Binding}" VerticalAlignment="Top" Width="198" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="3">
<sdk:Label Content="DocName:"></sdk:Label>
<TextBlock Text="{Binding Name}"></TextBlock>
<sdk:Label Content=" Author:"></sdk:Label>
<TextBlock Text="{Binding Author}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
最后:详细信息绑定是当用户点击列表中某一个实体标题属性的时候,自动显示其实体的详细信息给用户观看,注意在这里列表的数据源以及详细信息显示页的数据源都必须是CollectionViewSource类型的,其Xaml代码如下:
<!--第三种:使用绑定一个Detail详细信息-->
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="top"
Width="500" Height="240" Margin="112,294,188,66">
<ListBox Height="239" HorizontalAlignment="Left" Name="lbDetail"
VerticalAlignment="Top" Width="198" ItemsSource="{Binding}"
SelectedValuePath="Author" DisplayMemberPath="Name" />
<StackPanel x:Name="spDetail" Width="300" Height="200">
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock FontStyle="Italic" Text="{Binding Author}"/>
<TextBlock Text="{Binding Content}" />
<TextBlock Text="{Binding WriteDate}" />
</StackPanel>
</StackPanel>
本实例的后台代码如下,注意第三种详细信息的绑定方式:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//获取实体集合
ObservableCollection<Info> list = Info.GetList();
//第一种数据源赋值
this.lbCollection.DataContext = list;
//第二种数据源赋值
this.lbTemplate.DataContext = list;
//第三种数据源赋值
CollectionViewSource collection = new CollectionViewSource { Source = list };
this.lbDetail.DataContext = collection;
this.spDetail.DataContext = collection;
}
}
本实例定义一个实体以及实体集合如下:
public class Info
{
public string Name { get; set; }
public string Author { get; set; }
public string Content { get; set; }
public string WriteDate { get; set; }
public static ObservableCollection<Info> GetList()
{
ObservableCollection<Info> list = new ObservableCollection<Info>();
list.Add(new Info() { Name = "文章一", Author = "作者一", Content = "内容一", WriteDate = "2009-02-03" });
list.Add(new Info() { Name = "文章二", Author = "作者二", Content = "内容二", WriteDate = "2009-03-03" });
list.Add(new Info() { Name = "文章三", Author = "作者三", Content = "内容三", WriteDate = "2009-04-03" });
list.Add(new Info() { Name = "文章四", Author = "作者四", Content = "内容四", WriteDate = "2009-05-03" });
list.Add(new Info() { Name = "文章五", Author = "作者五", Content = "内容五", WriteDate = "2009-06-03" });
list.Add(new Info() { Name = "文章六", Author = "作者六", Content = "内容六", WriteDate = "2009-07-03" });
return list;
}
}
本实例采用Vs2010+Silverlight 4编写,如需源码请点击 SLBinding3.rar 下载。