如何在没有自定义渲染器的情况下更改Xamarin Forms Cross Platform中Listview所选项目的背景颜色

朱塞佩·特拉西(Giuseppe Terrasi)

如何在Xamarin Forms Cross Platform中更改列表视图所选项目的所选颜色?

我可以使用此问题中的答案更改它,如何在Xaml中更改ListView的背景颜色?

但首先出现的是所选项目的默认背景色(橙色)。

有什么建议?

我想知道为什么在2019年(2020年左右)没有一种简便的方法可以轻松地更改此基本属性和公共属性。

谢谢。

哈里希南

下面的代码非常适合我。但是,它会迭代整个ItemsSource,以重置先前选择的项目的背景。如果您想对其进行优化,则可以存储并重置它。希望这可以帮助。

<ListView x:Name="contactList" ItemsSource="{Binding PlatformsList}" ItemTapped="contactList_ItemTapped"
             VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label TextColor="Black" Margin="10,0" Text="{Binding PlatformName}" BackgroundColor="{Binding Background}" VerticalOptions="Center" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

private void contactList_ItemTapped(object sender, ItemTappedEventArgs e)
{
    var selectedItem = e.Item as PlatformInfo;
    selectedItem.ItemBackground = Color.Aqua;

    foreach(var item in this.contactList.ItemsSource)
    {
        if (item != selectedItem)
            (item as PlatformInfo).ItemBackground = Color.Transparent;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档