将多个项目设置为默认选择列表选择器 - 多选模式 - Windows Phone
本文关键字:选择器 Windows Phone 列表 模式 选择 项目 设置 默认 | 更新日期: 2024-10-26 07:18:41
我需要在列表视图中将一些项目设置为默认选中,我正在这样做是列表选择器加载的事件。这工作正常,接下来当用户更改这些选择时,我无法检索结果。选择更改事件在列表选取器加载事件之前触发,如果我在加载的方法中添加事件处理程序,将其从 XAML 中删除,则会出现异常
类型为"系统.无效操作异常"的未处理异常 发生在System.Windows.ni中.dll
这是我的代码。
private void interestms_Loaded(object sender, RoutedEventArgs e)
{
//selectedinterests is a string containing keys of selected interests seperated by commas.
object[] split1 = selectedinterests.Split(',');
//interest is a dictionary with total list of interests
var s = PhoneApplicationService.Current.State["interest"];
List<object> finallist = new List<object>();
var ss = (((System.Collections.Generic.Dictionary<string, string>)(s))).Keys;
List<object> arr = new List<object>((((System.Collections.Generic.Dictionary<string, string>)(s))).Values);
for (int k = 0; k < split1.Length; k++)
{
object getsel = arr[k];
finallist.Add(getsel);
}
interestms.SelectedItems = hello;
}
在 selectionChange 事件中,我获取的是已单击的项目,而不是那些已选中的项目,因此当我取消选中选中的项目时,该项目也会添加到 selectedItems 中。在这种情况下,我需要创建两个对象数组,一个包含总值集,另一个包含选定的项目,并删除两者中的公共项目。在这样做的时候选择更改的方法在加载事件之前被调用。
请帮忙。如果需要任何其他细节,我很乐意提供。
编辑:
private void interestms_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//edited interst is an array object
editedinterests.Add(e.AddedItems);
var s = PhoneApplicationService.Current.State["interest"];
List<object> arr = new List<object>((((System.Collections.Generic.Dictionary<string, string>)(s))).Values);
var listcommon = arr.Intersect(editedinterests);
}
试试这个
private void interestms_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(interestms.SelectedIndex==-1) return;
//Here may be you get all selected items no need to maintain two array if you get all selected items.
var listcommon = (cast as your type)interestms.SelectedItems;
interestms.SelectedIndex=-1;
}