문제

In my WP8 app, i am using the ListPicker control with multi selection option. In my page, i am loading the Listpicker data from a List of custom classes. And i can able to save what objects are user selected based by implementing the SummaryForSelectedItemsDelegate event and i am saving this info to Isolated storage for later access.

My main problem, how to select the above user selected option when user opened this page where this list picker is there?

I tried using the SelectionChanged event and try to select the object based on stored data, it didn't worked.

<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>

Code behind:

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";
    }
도움이 되었습니까?

해결책

In your page, override OnNavigatedTo and add all objects to userCountryList.SelectedItems:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    foreach (var o in GetSelectedObjects())
    {
        userCountryList.SelectedItems.Add(o);
    }
    base.OnNavigatedTo(e);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top