How to select collectionview's items in code level ?

Is there any way to select items of collection view programmatically.?

Best Answers

  • gigigigigigigigigigi ✭✭
    Accepted Answer

    @Dinesh_Official said:
    @gigigigigi , can you please share any simple sample?. Get confused with your last reply.

    In short what i understood is, you are having a property of PreSelectedItems which is an ObservableCollection and in that you are dynamically adding items into it at runtime which are not reflecting in view. Am I right?

    Yes! that was exactly my problem with my first attempt with my solution. But it's already working now, by getting the index of each OldSelectedItems in ObservableItems(The property which binds in collection view) using parsing, after getting the index of each item in OldSelectedItems I manually inserted in PreSelectedItems(Which the property that binds in CollectionView's SelectedItems property).

Answers

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @gigigigigi , can you please share any simple sample?. Get confused with your last reply.

    In short what i understood is, you are having a property of PreSelectedItems which is an ObservableCollection and in that you are dynamically adding items into it at runtime which are not reflecting in view. Am i right?

  • gigigigigigigigigigi Member ✭✭
    Accepted Answer

    @Dinesh_Official said:
    @gigigigigi , can you please share any simple sample?. Get confused with your last reply.

    In short what i understood is, you are having a property of PreSelectedItems which is an ObservableCollection and in that you are dynamically adding items into it at runtime which are not reflecting in view. Am I right?

    Yes! that was exactly my problem with my first attempt with my solution. But it's already working now, by getting the index of each OldSelectedItems in ObservableItems(The property which binds in collection view) using parsing, after getting the index of each item in OldSelectedItems I manually inserted in PreSelectedItems(Which the property that binds in CollectionView's SelectedItems property).

  • Dinesh_OfficialDinesh_Official Member ✭✭✭
    edited January 29

    @gigigigigi , okay great :) . this is because the instance of each data model get varied. So that, selected items are not reflecting in view.

  • gigigigigigigigigigi Member ✭✭

    @Dinesh_Official said:
    @gigigigigi , okay great :) . this is because the instance of each data model get varied. So that, selected items are not reflecting in view.

    Hello, I just want to ask if you know why the collectionview's selectionchanged event is like looping when you triggering the event.

    For example*
    If you have 5 selected items in multiple selection mode, the selectionchanged event will be called 6x. Why 6x?, because on my observation in the 1st loop the currentselection property of SelectionChangedEventArgs is 0 then 1,2,3,4,5.
    I'm focusing on UWP platform and I don't use SelectionChangedCommand and SelectionChangedCommandParameter

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @gigigigigi , Can you share how you are added selected items into the bind property of CollectionView's SelectedItems.

    If you are adding through loop, CurrentSelection property value gets changed which forces to raise the SelectionChangedEventArgs.

    Better copy the whole collection once.

  • gigigigigigigigigigi Member ✭✭

    @Dinesh_Official said:
    @gigigigigi , Can you share how you are added selected items into the bind property of CollectionView's SelectedItems.

    If you are adding through loop, CurrentSelection property value gets changed which forces to raise the SelectionChangedEventArgs.

    Better copy the whole collection once.

        private void AddonProductCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedList = e.CurrentSelection;
    
             _guestViewModel.Order.OrderDetails[_guestViewModel.SelectedIndex].Addons.Clear();
    
            foreach (var selected in selectedList)
            {
        _guestViewModel.AddRawAddon((DetailAddonsViewModel)selected);
            }
        }
    

    Order.OrderDetails is my ObservableCollection

    I didn't paste some codes because most of them are condition only but this is my way on how to get the selected items in multiple selection mode.

    Basically, in my AddRawAddon method, my codes are Order.OrderDetails.Add(param);

    As you can see every time the event will trigger or called I always clear my ObservableCollectionProperty then add the new set of selectedList because like I said before the event method was called like looping style and if I select the first item the event method will proc 2x because the CurrentSelection will start to 0 then 1 and so on.

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @gigigigigi , because your clear the values and again adding the selected items again and again. So, that loop of selectionchanged event executes.

  • gigigigigigigigigigi Member ✭✭

    @Dinesh_Official said:
    @gigigigigi , because your clear the values and again adding the selected items again and again. So, that loop of selectionchanged event executes.

    No no, I have separate ObservableCollection for my CollectionView item source, the loop that I'd told you before was in the whole AddonProductCollectionView_SelectionChanged method event. If you will recreate it in debug mode probably you will encounter the issue. It will start on "var selectedList = e.CurrentSelection;" line base on the counts of your selected items.

Sign In or Register to comment.