c# - 如何设置 SelectionMode 为 multiple 的 Listpicker 项目

标签 c# windows-phone-8 listpicker

在我的 WP8 应用程序中,我使用带有多选选项的 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";
    }

最佳答案

在您的页面中,重写 OnNavieratedTo 并将所有对象添加到 userCountryList.SelectedItems:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    foreach (var o in GetSelectedObjects())
    {
        userCountryList.SelectedItems.Add(o);
    }
    base.OnNavigatedTo(e);
}

关于c# - 如何设置 SelectionMode 为 multiple 的 Listpicker 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691957/

相关文章:

c# - 对象深度克隆实现

c# - EF Core 5 - 更新多对多关系中的实体会引发 "Violation of PRIMARY KEY"错误

windows-phone-7 - ListPicker - 如何在全页 View 打开时显示当前选择

c# - 如何清除 .NET 的反射缓存?

C# Mysql EF6 代码优先 System.ComponentModel.Win32Exception 文件未找到

windows-phone-8 - 允许使用相机按钮打开我的应用程序

windows - 是否可以在 PositionChanged 事件处理程序中更改 Geolocator 的 DesiredAccuracy 和 ReportInterval?

image - Windows Phone 8 - 在后台生成锁屏图像

windows-phone-8 - 未使用 Windows Phone 8 Listpicker ItemTemplate