[英]How to get multiple selected items from ListPicker and display them in MessageBox
Here is my code for the list of the items in a ListPicker. 这是我的ListPicker中项目列表的代码。 What I am trying to do is select one or more options and then after I press submit button I would like to display a MessageBox with the selected Items separated by commas.
我想做的是选择一个或多个选项,然后按“提交”按钮后,我想显示一个MessageBox,其中所选项目用逗号分隔。 I also would like to store this value of the items selected to the database but, the first I am trying to do is populate the data into MessageBox.
我还想将所选项目的此值存储到数据库中,但是,我首先要做的是将数据填充到MessageBox中。
lstPickerType.Items.Add("Aircrafts");
lstPickerType.Items.Add("Boats");
lstPickerType.Items.Add("Cars");
lstPickerType.Items.Add("Helicopters");
lstPickerType.Items.Add("Electric Powered");
lstPickerType.Items.Add("Gas Powered");
Here is the code that I have got to create a string from the list that is then displayed when the ListPicker is collapsed. 这是我必须从列表中创建一个字符串的代码,当ListPicker折叠时,该列表将显示出来。
private void lstPickerType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
lstPickerType.SummaryForSelectedItemsDelegate = SummarizeItems;
}
private string SummarizeItems(IList items)
{
if (items != null && items.Count > 0)
{
string summarizedString = "";
for (int i = 0; i < items.Count; i++)
{
summarizedString += (string)items[i];
// if the item is not last coma is added
if (i != items.Count - 1)
summarizedString += ", ";
}
return summarizedString;
}
else
return "Nothing selected";
}
Finally for the button to display the MessageBox i have following code. 最后,用于显示MessageBox的按钮具有以下代码。
private void btnAddLocation_Click(object sender, RoutedEventArgs e)
{
foreach (var item in this.lstPickerType.SelectedItems)
{
var items = new List<object>();
MessageBox.Show(items.ToString());
}
I would really appreciate if anyone could help me to solve this issue. 如果有人可以帮助我解决此问题,我将不胜感激。 Thank You.
谢谢。
I'm a bit rusty of C#, and i don't have visual studio in this pc, but I would achieve your result without the selectionchanged event in the listpicker. 我对C#有点生锈,并且我在这台PC中没有Visual Studio,但是如果没有列表选择器中的selectionchanged事件,我会达到您的结果。
Trythis code in the button click: Edit: a cast was missing. 尝试在按钮中单击以下代码:编辑:演员表丢失。
private void btnAddLocation_Click(object sender, RoutedEventArgs e)
{
string r = "";
for (int i=0; i<this.lstPickerType.SelectedItems.Count; i++)
{
r += ((ListPickerItem)this.lstPickerType.SelectedItems[i]).Content;
if (i != this.lstPickerType.SelectedItems.Count - 1)
r += ", ";
}
MessageBox.Show(r);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.