WinRT Xaml 列表视图项交替的背景色

本文关键字:背景色 Xaml 列表 视图 WinRT | 更新日期: 2023-09-27 18:33:38

我需要使ListView交替其行的背景颜色。我尝试了很多东西,但对我没有任何用处。

第一行必须为白色背景,第二行必须为

黑色,第三行必须为白色,依此类推。

有谁知道一个简单的方法吗?我相信有一个简单的解决方案,因为它是一个常规功能。

我正在使用 C# 开发 WinRT 应用程序,并尝试了选择器、转换器。

WinRT Xaml 列表视图项交替的背景色

如果列表视图已填充,则可以使用以下代码在运行时执行此操作。 在 ListView 的模板中可能需要其他格式设置才能进行填充,以及不需要的内容。

        int i = 1;
        foreach (ListViewItem item in listView.Items)
        {
            if (i % 2 == 0)
            {
                item.Background = new SolidColorBrush(Windows.UI.Colors.Black);
                item.Foreground = new SolidColorBrush(Windows.UI.Colors.White);
            }
            else
            {
                item.Background = new SolidColorBrush(Windows.UI.Colors.White);
                item.Foreground = new SolidColorBrush(Windows.UI.Colors.Black);
            }
            i++;
        }

例如:

<Grid Background="{Binding color_background}"/>

只需将属性添加到 ListView 中的元素集合