首页 文章

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

提问于
浏览
1

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

System.Windows.ni.dll中出现未处理的“System.InvalidOperationException”类型异常

这是我的代码..

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 回答

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

相关问题