Search code examples
listviewxamarin.formsxamarin.androidhighlight

Highlighting of selected item of ListView if background color of stack layout is set


I was trying to check if selected item of listview (made of viewcell) is being highlighted when it is tapped or selected. I noticed if stacklayout is used in the viewcell and if the background color of that stacklayout is set, then highlight is not functioning. When, I removed the backgroundcolor property, Highlight becomes functional.

Is there a way to bypass that limitation?

<ContentPage.Content>
    <StackLayout Orientation="Vertical" Padding="5"> 
        <ListView x:Name="List" SelectionMode="Single"  ItemsSource="{Binding ListOfStored}" RowHeight="100" SeparatorColor="#2EC022"
                  SeparatorVisibility="Default" HasUnevenRows="True" SelectedItem="{Binding SelectedEntry, Mode=OneWayToSource}" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell  >
                        <StackLayout Orientation="Vertical"  Padding="5" ***BackgroundColor="LightGray">***

                            <Label Text="{Binding Id}"  HorizontalOptions="Start" LineBreakMode="NoWrap" BackgroundColor="LightGray" />
                            <Label Text="{Binding Definition}" HorizontalOptions="StartAndExpand" 
                                    HorizontalTextAlignment="Start" MaxLines="10" LineBreakMode="WordWrap"/>
                            <!--<Label Text="Examples:" FontAttributes="Bold" HorizontalOptions="StartAndExpand"/>
                            <Label Text="{Binding Example1}" HorizontalOptions="StartAndExpand"
                                    MaxLines="10" LineBreakMode="WordWrap"/>
                            <Label Text="{Binding Example2}" HorizontalOptions="StartAndExpand"
                                    MaxLines="10" LineBreakMode="WordWrap"/>-->
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <StackLayout Orientation="Horizontal" VerticalOptions="End" Padding="5" >
            <Button Text="GetFullList" Command="{Binding GetList}" VerticalOptions="Center"/>
            <Button Text="Delete" VerticalOptions="Center" Command="{Binding DeleteEntry }"/>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>


Solution

  • You can use the Grid or Frame that wrap a view with a border(Stacklayout) to do this.

     <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="5" BackgroundColor="Transparent">
                                <StackLayout BackgroundColor="AliceBlue">
                                    <Label
                                        FontSize="Medium"
                                        Text="{Binding FullName}"
                                        TextColor="Orange" />
    
                                </StackLayout>
                            </Grid>
    
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
    
    <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame Padding="5" BackgroundColor="Transparent">
                                <StackLayout BackgroundColor="AliceBlue">
                                    <Label
                                        FontSize="Medium"
                                        Text="{Binding FullName}"
                                        TextColor="Orange" />
    
                                </StackLayout>
                            </Frame>
    
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
    

    enter image description here