Microsoft Silverlight for Windows Phone Toolkit是由微软Silverlight团队为Windows Phone 7开发的一组Silverlight组件包。它在Windows Phone 7标准组件之外提供了一套控件和组件包。其目标是更加快速地提供控件和新功能。
首先 ListPicker控件 来自于Toolkit组件包 ,所以首先得安装Toolkit . (可以在官网下载安装)
安装后,可以在项目中引用 Microsoft.Phone.Controls.Toolkit.dll文件
在需要使用LIstPicker控件的xaml页面中输入命名空间 xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
然后就可以使用了。。
应用之省市级连列表
代码如下:
MainPage.xaml 代码
<toolkit:ListPicker Header="选择省份:" Name="listPicker_Province"
ItemsSource="{Binding}"
Grid.Row="1"
SelectionChanged="listPicker_Province_SelectionChanged">
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="24" Text="{Binding provinceName}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="24" Text="{Binding provinceName}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
<toolkit:ListPicker Header="选择地市:" Name="listPicker_City"
ItemsSource="{Binding}"
SelectionChanged="listPicker_City_SelectionChanged"
Grid.Row="2" >
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="24" Text="{Binding cityName}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="24" Text="{Binding cityName}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
MainPage.xaml.cs 代码
//初始化省份列表
public void InitListPicker_Province()
{
List<Province> provinceList = new List<Province>();
provinceList = ProvinceList.getInstance().getProvinceList();
this.listPicker_Province.ItemsSource = provinceList;
}
/// <summary>
/// 省份选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listPicker_Province_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectIndex = listPicker_Province.SelectedIndex;
//provinceId = selectedProvince.provinceId; //选中省份Id
if (selectIndex != -1)
{
Province selectedProvince = (Province)listPicker_Province.SelectedItem;
provinceId = selectedProvince.provinceId;
//绑定地市列表
List<City> cityList = new List<City>();
cityList = CityList.getInstance().getCityListSameProvinceId(provinceId);
listPicker_City.ItemsSource = cityList;
}
}
/// <summary>
/// 地市选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listPicker_City_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectIndex = listPicker_City.SelectedIndex;
if (selectIndex!= -1)
{
City selectedCity = (City)listPicker_City.SelectedItem;
cityId = selectedCity.cityId; //选中地市Id
}
}