如何在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] 删除。
我来说两句