如何在 Xamarin Forms Cross Platform 中更改 ListView 选定项的选定颜色?
我可以使用这个问题的答案来更改它 How can I change background color of a ListView in Xaml?
但第一次出现时,所选项目具有默认背景颜色(橙色)。
有什么建议吗?
我想知道为什么在 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;
}
}
关于c# - 如何在没有自定义渲染器的情况下更改 Xamarin Forms Cross Platform 中 ListView 所选项目的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59181474/