Question

I need to set some items as selected by default in list view,I am doing this is listpicker loaded event.This works fine,Next when user changes those selections, I am unable to retrieve the results.SelectionChanged event gets fired before listpicker loaded event, If I add event handler in loaded method, removing it from XAML, I m getting the exception

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.ni.dll

Here is my code..

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

On selectionChange event , I am getting the items that have been clicked,not those items that have been checked, so When I uncheck a checked item, that item also gets added in selectedItems. In this case I need to create two object arrays,one containing the total set of values and other the selected Items and remove the common items in both.In doing so selectionChanged method gets called before loaded event.

Please help.In case any other detail is required,I would be glad to provide..

EDIT:

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);
    }
Was it helpful?

Solution

Try this

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top