【问题标题】:Setting multiple items as selected by default list picker- multiple selection mode -windows phone将多个项目设置为默认选中列表选择器-多选模式-windows手机
【发布时间】:2013-12-18 13:29:38
【问题描述】:

我需要在列表视图中将某些项目设置为默认选中,我正在这样做是 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);
    }

【问题讨论】:

  • 能否提供选择更改事件的代码sn-ps
  • @Jaihind 是的,我已经添加了它..

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


【解决方案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;
}

【讨论】:

  • 我试过这段代码,这个事件在interestms_loaded事件之前被触发并带我到根框架导航失败调试器..当我删除interestms.SelectedIndex=-1;我得到system.invalid操作异常
  • @PrasannaAarthi 从 xaml 中移除 interestms_Loaded 事件并在 InitializeComponent() 之后初始化 interestms_Loaded;代码中的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 2019-04-29
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2017-04-28
相关资源
最近更新 更多