将多个项目设置为默认选择列表选择器 - 多个选择模式 - 窗口电话

时间:2013-12-18 13:29:38

标签: c# windows-phone-7 windows-phone-8 listpicker

我需要在列表视图中默认设置一些项目,我这样做就是listpicker加载的事件。这很好用,接下来当用户更改这些选择时,我无法检索结果.SelectionChanged事件被触发之前listpicker加载事件,如果我在加载方法中添加事件处理程序,从XAML中删除它,我得到异常

  

未处理的类型' System.InvalidOperationException'   发生在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中。在这种情况下,我需要创建两个对象数组,一个包含总值集和其他所选项,并删除两者中的公共项。 selectionChanged方法在加载事件之前被调用。

请帮助。如果需要任何其他细节,我很乐意提供..

编辑:

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);
    }

1 个答案:

答案 0 :(得分:1)

试试这个

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