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

问题描述 投票:0回答:2

我正在尝试检查列表视图(由viewcell构成)的选定项目是否在被点击或选中时被突出显示。我注意到,如果在视单元中使用了stacklayout,并且设置了该stacklayout的背景色,那么高光将无法正常工作。当我删除backgroundcolor属性时,Highlight起作用了。

是否有办法绕过该限制?

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

listview xamarin.forms xamarin.android highlight
2个回答
0
投票

使用框架并放入您的StackLayout,然后设置框架的BackgroundColor = transparent和Padding = 0。


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>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.