简体   繁体   中英

how to clear collectionview selection in xamarin

On Page#1, i have a collectionview with text/string items list. If you tap aa item, it will get the current tapped text/string and send to Page#2. sound simple

issue I am having: if you tap on item#1, then it will send item#1 to page2, this part working fine. But on page#2, if you hit back button, and tap on item#1 again.. than nothing happens, it doesnt go to page#2

Fix: i think i need to somehow clear tap selection, and than send the item to page#2. but im not sure how to do this

On Page#1, i have a simple collectionview. Collection view contains text/string list

        <CollectionView ItemsSource="{Binding MyListItem}"
                        SelectionMode="Single"
                        SelectionChanged="CollectionView_SelectionChanged">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ContentView>
                        <!-- Body -->
                        <Grid Padding="0">
                            <Frame CornerRadius="3" BorderColor="#f2f4f5" HasShadow="True">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="icon_about"
                                               WidthRequest="25"  />
                                    <StackLayout VerticalOptions="Center">
                                        <Label VerticalOptions="Center"
                                                    FontSize="16" 
                                                    Text="{Binding .}" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>

back end code to handle selection is:

    private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var previous = e.PreviousSelection.FirstOrDefault();
        var current = e.CurrentSelection.FirstOrDefault();

        var route = $"{ nameof(Page2) }?URLCardType={current}";
        await Shell.Current.GoToAsync(route);
        
        //clear selection 
        ((CollectionView)sender).SelectedItem = null;
    }

Update ((CollectionView)sender).SelectedItem = null; fixed the issue of clearing selected item but CollectionView_SelectionChanged method is get run twice on single tap. why? this is all the code i have

@jason thanks, this worked for me. i just had to check if selection item is null than do nothing

  private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
         var MyCollectionView = sender as CollectionView;  
            if (MyCollectionView.SelectedItem == null)
                return;

        var previous = e.PreviousSelection.FirstOrDefault();
        var current = e.CurrentSelection.FirstOrDefault();

        var route = $"{ nameof(Page2) }?URLCardType={current}";
        await Shell.Current.GoToAsync(route);
        
        //clear selection 
       MyCollectionView.SelectedItem = null;
    }

Here is a simple solution that worked for me. Setting to null did not do the trick, it was causing all kinds of weird stuff.

  private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var itemselected = e.CurrentSelection[0] as ProductsItem;
       if (itemselected != null)
        Shell.Current.GoToAsync(nameof(SelectedProductPage));

       //Setting the selected item to an emptyviewproperty after navigation
        CollectionList.SelectedItem = SelectableItemsView.EmptyViewProperty;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM