Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to keep item selected in xamarin forms?

$
0
0

Hi am using LIstView, when I tap on the item background of that item will be default some color will set. If I click on another item then both items background will be set. What I need to do when I click on second item first Item should not be selected, only clicked item should be active. How to do it?

Find below for xaml file code
<ListView ItemsSource="{Binding MainMenues}" SeparatorVisibility="None" HasUnevenRows="true"  SelectionMode="Single">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical">
                            <Grid Padding="15">
                                <Label Text="{Binding MainMenuTitle}"  FontSize="16" VerticalOptions="Center" HorizontalOptions="Start"/>
                                <Image x:Name="im" Source="{Binding ArrowIconSource}" IsVisible="{Binding IsSubmenu}" HeightRequest="15" WidthRequest="15" VerticalOptions="Center" 
                                           HorizontalOptions="End">
                                </Image>
                                <Grid.GestureRecognizers>
                                    <TapGestureRecognizer 
                                                Tapped="Handle_Tapped"
                                                NumberOfTapsRequired="1" />
                                </Grid.GestureRecognizers>
                            </Grid>
                            <Grid IsVisible="{Binding IsChildrenVisible}" HeightRequest="{Binding ChildrenRowHeightRequest}">
                                <ListView ItemsSource="{Binding Submenues}" SelectionMode="Single" ItemTapped="ListView_ItemTapped" SeparatorVisibility="None" SelectionMode="Single" HasUnevenRows="false">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <Grid Padding="10">
                                                    <Label Text="{Binding SubMenuTitle}"  VerticalOptions="Center" HorizontalOptions="Start" />
                                                    <Image IsVisible="False" Source="rightarrow.png" HeightRequest="12" WidthRequest="12" VerticalOptions="Center" 
                                                                HorizontalOptions="End"/>
                                                </Grid>
                                            </ViewCell>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </Grid>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

C# Code
 async void Handle_Tapped(object sender, EventArgs e)
        {
            try
            {
                Grid g = sender as Grid;
                Image im = g.Children[1] as Image;
                await im.RotateTo(180);
                Label label = g.Children[0] as Label;
                g.BackgroundColor = Color.Red;

                viewModel.ShowCities(label.Text);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            try
            {
                var s = e.Group as SubMenu;
                switch (s.SubMenuTitle.ToLower())
                {
                    case "mobile":
                        await RootPage.NavigateFromMenu(5);
                        break;
                    case "sub menu":
                        await RootPage.NavigateFromMenu(4);
                        break;
                }   
            }
            catch (Exception ex)
            {

            }
            ((ListView)sender).SelectedItem = null;
        }

In both, I need to select only one listview at a time.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>