在我的WP8应用程序中,我使用带有多选选项的ListPicker控件。在我的页面中,我正在从自定义类列表中加载Listpicker数据。我可以通过实现SummaryForSelectedItemsDelegate事件来保存用户选择的对象,并将此信息保存到隔离存储中以供以后访问。
我的主要问题是,当用户打开此列表选择器所在的页面时,如何选择上述用户选择的选项?
我尝试使用SelectionChanged事件并尝试根据存储的数据选择对象,但它没有用。
<toolkit:ListPicker x:Name="userCountryList" ItemsSource="{Binding CountryList}" Header="Choose a country or region:" SelectionMode="Multiple" FullModeItemTemplate="{StaticResource DataTemplate2}" />
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DataTemplate2">
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" FontSize="28" TextWrapping="Wrap" Text="{Binding CountryName}" VerticalAlignment="Center" Width="Auto"/>
</StackPanel>
</DataTemplate>
代码背后:
var brushes = new List<CustomClass>();
brushes.Add(new CustomClass { CountryCode = "US", CountryName = "United States" });
brushes.Add(new CustomClass { CountryCode = "FR", CountryName = "France" });
brushes.Add(new CustomClass { CountryCode = "DE", CountryName = "Germany" });
brushes.Add(new CustomClass { CountryCode = "CA", CountryName = "Canada" });
userCountryList.SummaryForSelectedItemsDelegate = SummarizeItems;
userCountryList.ItemsSource = brushes;
private string SummarizeItems(IList items)
{
if (items != null && items.Count > 0)
{
string summarizedString = "";
if (items != null)
for (int i = 0; i < items.Count; i++)
{
summarizedString += ((CustomClass )items[i]).CountryCode;
// If not last item, add a comma to seperate them
if (i != items.Count - 1)
summarizedString += ", ";
}
if (!AppSettings.Default.UserSelectedMarkets.Equals(summarizedString))
AppSettings.Default.UserSelectedMarkets = summarizedString;
return summarizedString;
}
else
return "Nothing selected";
}
答案 0 :(得分:3)
在您的页面中,覆盖OnNavigatedTo并将所有对象添加到userCountryList.SelectedItems:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
foreach (var o in GetSelectedObjects())
{
userCountryList.SelectedItems.Add(o);
}
base.OnNavigatedTo(e);
}