書名:WPF專業(yè)編程指南
作者:李應(yīng)保
出版社:電子工業(yè)出版社
出版時間:2010-01
ISBN:9787121100116
數(shù)據(jù)綁定
一、對集合對象的綁定
本節(jié)將討論對數(shù)據(jù)集合的綁定。
在實際應(yīng)用中,我們常常面對的是集合對象。
比如,之前對銀行賬戶的討論,應(yīng)用程序要管理的常常是成千上萬個賬戶。
這些賬戶的特點(diǎn)是每個賬戶的數(shù)據(jù)結(jié)構(gòu)是相同的,在關(guān)系數(shù)據(jù)庫中,這些數(shù)據(jù)可能位于同一個表內(nèi)。應(yīng)用程序在管理客戶的地址時,情況也是類似的。每個客戶都有至少一個地址,這些地址的數(shù)據(jù)結(jié)構(gòu)也是相同的。
由此可見,具有相同數(shù)據(jù)結(jié)構(gòu)的數(shù)據(jù)表非常廣泛。
下面以地址為例來討論聚合對象的綁定。為此,寫了一個簡單的類AddressInfo:
namespace Yingbao.Chapter11.AddressListBinding
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
public class AddressInfo
{
private string streetNo;
private string streetName;
private string city;
private string province;
private string country;
private string postCode;
public string StreetNo
{
get { return this.streetNo; }
set { this.streetNo = value; }
}
public string StreetName
{
get { return this.streetName; }
set { this.streetName = value; }
}
public string City
{
get { return this.city; }
set { this.city = value; }
}
public string Province
{
get { return this.province; }
set { this.province = value; }
}
public string Country
{
get { return this.country; }
set { this.country = value; }
}
public string PostCode
{
get { return this.postCode; }
set { this.postCode = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(Country)
.Append(" ").Append(Province).Append(" ")
.Append(City).Append(" ").Append(StreetName)
.Append(" ").Append(streetNo);
return sb.ToString();
}
public AddressInfo()
{
}
}
這個類描述用戶的地址信息,包括國家、省、市、街道和郵政編碼等信息。
在C#中,要管理這個地址數(shù)據(jù)的集合,最簡單的方法是使用List(在第10章,我們用ObservableCollection),例如可以用AddressList來管理多個地址:
public class AddressList :List<AddressInfo>
{
public AddressList()
{
}
}
現(xiàn)在我們把AddressList放到XAML的資源中,下面的XAML創(chuàng)建了三個地址,并放在AddressList中:
<Window.Resources>
<src:AddressList x:Key="addressList">
<src:AddressInfo Country="中國" City="北京"
StreetName="學(xué)院路" StreetNo="178"/>
<src:AddressInfo Country="中國" Province="安徽"
City="合肥" StreetName="長江路" StreetNo="355"/>
<src:AddressInfo Country="中國" Province="安徽" City="蕪湖"
StreetName="九華山路" StreetNo="23"/>
</src:AddressList>
</Window.Resources>
我們可以使用ListBox來顯示AddressList中的信息,可以直接把ListBox中的ItemsSource綁定到AddressInfo上,把ListBox的DataContext設(shè)為AddressList:
<ListBox Name="lbAddress" Grid.Column ="0" Grid.Row ="0"
Grid.ColumnSpan ="2" ItemsSource="{Binding}"
DataContext ="{StaticResource addressList}" />
這樣,ListBox就會顯示AddressInfo的信息。在未指定顯示AddressInfo的特定屬性的情況下,ListBox會調(diào)用AddressInfo的ToString()方法來作為每個條目的默認(rèn)顯示。
下面是完整的XAML:
<Window x:Class="Yingbao.Chapter11.AddressListBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Yingbao.Chapter11.AddressListBinding"
Title="綁定地址列表" Height="350" Width="400">
<Window.Resources>
<src:AddressList x:Key="addressList">
<src:AddressInfo Country="中國" City="北京"
StreetName="學(xué)院路" StreetNo="178"/>
<src:AddressInfo Country="中國" Province="安徽" City="合肥"
StreetName="長江路" StreetNo="355"/>
<src:AddressInfo Country="中國" Province="安徽" City="蕪湖"
StreetName="九華山路" StreetNo="23"/>
</src:AddressList>
</Window.Resources>
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition Width ="120"/>
<ColumnDefinition Width ="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height ="100" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
<RowDefinition Height ="25" />
</Grid.RowDefinitions>
<ListBox Name="lbAddress" Grid.Column ="0" Grid.Row ="0"
Grid.ColumnSpan ="2" ItemsSource="{Binding}"
DataContext ="{StaticResource addressList}" />
</Grid>
</Window>
上面的程序的運(yùn)行結(jié)果為:
