gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:14:03 25 4
gpt4 key购买 nike

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com