更改listview项目的背景颜色

时间:2017-10-30 09:36:05

标签: c# xaml listview uwp

我想更改listview备用行的背景颜色。我通过ObservableCollection将值绑定到listview。这样我就无法遍历listview项目。它显示:

  

`System.InvalidCastException:'无法转换类型' xx.StudentClass'的对象键入' Windows.UI.Xaml.Controls.ListViewItem'。'

ObservableCollection<StudentClass> StudentData = new ObservableCollection<StudentClass>();
var statement = connection.Prepare("SELECT name,ID from student_details");

while (!(SQLiteResult.DONE == statement.Step()))
{
    if (statement[0] != null)
    {                     
        StudentClass c1 = new StudentClass() { studentName= statement[0].ToString, studentID= statement[1].ToString};
        StudentData.Add(c1);
    }
}

StudentListview.ItemsSource = StudentData;
ChangeBgColor();

private void ChangeBgColor()
{
    int counter = 1;

    foreach (ListViewItem item in this.StudentListview.Items)
    {
        if (counter % 2 == 0)
        {
            item.Background = new SolidColorBrush(Colors.Orange);
        }
        else
        {
            item.Background = new SolidColorBrush(Colors.OrangeRed);
        }
        counter++;
    }
}

<ListView x:Name="StudentListview" Visibility="Collapsed"    VerticalAlignment="Top" HorizontalAlignment="Right" Height="250px"  Width="550px">
  <ListView.ItemTemplate >
    <DataTemplate>
      <Grid>
        <StackPanel Orientation="Vertical" >
          <StackPanel Orientation="Horizontal">
            <TextBlock Foreground="Black" Text="{Binding studentName}"  FontSize="20"  Width="350px" TextWrapping="Wrap"></TextBlock>
          </StackPanel>
          <StackPanel Orientation="Horizontal">                                                              
            <TextBlock Foreground="Black" Text="{Binding  studentID}"  FontSize="20" Width="350px" TextWrapping="Wrap" ></TextBlock>
          </StackPanel>
      </Grid>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

1 个答案:

答案 0 :(得分:1)

如果您希望以未来的方式解决此问题,我建议您创建一个可以处理此问题的新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" />