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

标签 c# visual-studio xaml xamarin xamarin.forms

如何在 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/

相关文章:

c# - 更改 Xamarin.Forms.Maps 中缩放按钮 (+-) 的位置

c# - 在没有对话框的情况下以编程方式保存 MHT

C#:专用模板方法 - 错误:类型 '...' 已经使用相同的参数类型定义了一个名为 '...' 的成员

c++ - 使用 UNREFERENCED_PARAMETER 宏

regex - 如何在 Visual Studio 中替换部分匹配的正则表达式字符串

wpf - 如何为单一类型指定两个数据模板?

c# - 单个 ViewModel 的多个 View

c# - 如何在代码中设置绑定(bind)?

c# - 在 C# 中以另一种形式更改标签的文本?

c++ - 将 Windows 上的 FreeType 2.6.3 编译为 .dll