如果设置了堆栈布局的背景颜色,则突出显示 ListView 的选定项目

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

我试图检查列表视图(由 viewcell 制成)的选定项目在被点击或选择时是否被突出显示。我注意到如果在视单元中使用了 stacklayout,并且如果设置了该 stacklayout 的背景颜色,则突出显示不起作用。当我删除背景色 属性 时,突出显示功能。

有没有办法绕过这个限制?

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

使用frame,把你的StackLayout放在里面,设置frame的BackgroundColor = transparent和Padding=0。

您可以使用用边框包裹视图的网格或框架 (Stacklayout) 来执行此操作。

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