在我的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";
}
最佳答案
在页面中,重写OnNavigatedTo并将所有对象添加到userCountryList.SelectedItems:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
foreach (var o in GetSelectedObjects())
{
userCountryList.SelectedItems.Add(o);
}
base.OnNavigatedTo(e);
}
关于c# - 如何在SelectionMode为多个的情况下设置Listpicker项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18691957/