2

在我的 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";
    }
4

1 回答 1

3

在您的页面中,覆盖 OnNavigatedTo 并将所有对象添加到 userCountryList.SelectedItems:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    foreach (var o in GetSelectedObjects())
    {
        userCountryList.SelectedItems.Add(o);
    }
    base.OnNavigatedTo(e);
}
于 2013-09-09T08:52:31.173 回答