C# 更改listview项目的背景色

C# 更改listview项目的背景色,c#,xaml,listview,uwp,C#,Xaml,Listview,Uwp,我想更改listview备用行的背景色。我通过ObservableCollection将值绑定到listview。这样我就不能遍历listview项了。它表明: `System.InvalidCastException:“无法将类型为'xx.StudentClass'的对象强制转换为类型为'Windows.UI.Xaml.Controls.ListViewItem'。” ObservableCollection StudentData=新的ObservableCollection(); var语

我想更改listview备用行的背景色。我通过ObservableCollection将值绑定到listview。这样我就不能遍历listview项了。它表明:

`System.InvalidCastException:“无法将类型为'xx.StudentClass'的对象强制转换为类型为'Windows.UI.Xaml.Controls.ListViewItem'。”

ObservableCollection StudentData=新的ObservableCollection();
var语句=connection.Prepare(“从学生详细信息中选择姓名、ID”);
而(!(SQLiteResult.DONE==statement.Step())
{
if(语句[0]!=null)
{                     
StudentClass c1=new StudentClass(){studentName=语句[0]。ToString,studentID=语句[1]。ToString};
学生数据。添加(c1);
}
}
StudentListview.ItemsSource=StudentData;
ChangeBgColor();
私有void ChangeBgColor()
{
int计数器=1;
foreach(此.StudentListview.Items中的ListViewItem项)
{
如果(计数器%2==0)
{
item.Background=新的SolidColorBrush(颜色为橙色);
}
其他的
{
item.Background=新的SolidColorBrush(颜色为橙色);
}
计数器++;
}
}

如果您希望以一种经得起未来考验的方式解决此问题,我建议创建一个新的
ListView
控件来处理此问题

我是这样做的,首先用以下属性定义新控件

public class AlternatingListView : ListView
{
    public static readonly DependencyProperty OddRowBackgroundProperty = DependencyProperty.Register(
        nameof(OddRowBackground),
        typeof(Brush),
        typeof(AlternatingListView),
        new PropertyMetadata(null));

    public static readonly DependencyProperty EvenRowBackgroundProperty = DependencyProperty.Register(
        nameof(EvenRowBackground),
        typeof(Brush),
        typeof(AlternatingListView),
        new PropertyMetadata(null));

    public Brush OddRowBackground
    {
        get { return (Brush)GetValue(OddRowBackgroundProperty); }
        set { SetValue(OddRowBackgroundProperty, (Brush)value); }
    }

    public Brush EvenRowBackground
    {
        get { return (Brush)GetValue(EvenRowBackgroundProperty); }
        set { SetValue(EvenRowBackgroundProperty, (Brush)value); }
    }

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        base.PrepareContainerForItemOverride(element, item);
        ListViewItem listViewItem = element as ListViewItem;

        if (listViewItem == null)
        {
            return;
        }

        int index = IndexFromContainer(element);
        listViewItem.Background = (index + 1) % 2 == 1 ? OddRowBackground : EvenRowBackground;
    }
}
有了所有这些,您可以添加控制XAML的控件并定义所需的颜色

  <controls:AlternatingListView x:Name="ListView"
                                ItemsSource="{x:Bind Items}"
                                EvenRowBackground="SlateGray"
                                OddRowBackground="White" />


它显示windows通用项目不支持AlternatingListView。控件是您创建的吗?如果是这样,并且仍然得到这个结果,那么您需要在xaml中添加正确的名称空间引用!是,如上所述创建控件。如何添加该名称空间?已经有x:Class=“projectname.MainPage”。我在mainpage.cs页面复制了上面两个类在我的示例中,控件是在控件命名空间中定义的。。。因此,在我的xaml中,我有以下关于:xmlns:controls=“using:**”的内容,但我现在看到,也许我最好为您更改示例。。。你只需要一节课。因此,将公共部分类AlternatingListView:ListView更改为公共类AlternatingListView:ListView,并将所有类代码复制粘贴到这个1类中。(在我的项目中,由于某些UI原因,我分开了)重新格式化代码后,我发现您缺少外部
堆栈面板上的结束标记。
。如果您需要扩展所做的控件以处理悬停和按下时的不同颜色,这个问题的答案可能会很有用-
  <controls:AlternatingListView x:Name="ListView"
                                ItemsSource="{x:Bind Items}"
                                EvenRowBackground="SlateGray"
                                OddRowBackground="White" />