【问题标题】:Change the background color of listview items更改列表视图项的背景颜色
【发布时间】:2017-10-30 09:36:05
【问题描述】:

我想更改列表视图交替行的背景颜色。我通过 ObservableCollection 将值绑定到列表视图。这样我就无法遍历 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>

【问题讨论】:

  • 重新格式化您的代码后,我发现您在外部 StackPanel 上缺少一个结束标记。
  • 如果您需要扩展控件以处理悬停和按下时的不同颜色 - here

标签: c# xaml listview uwp


【解决方案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" />

【讨论】:

  • 它显示 Windows 通用项目不支持 AlternatingListView。
  • 您创建了控件?如果是这样并且仍然得到这个,您需要在您的 xaml 中添加正确的命名空间引用!
  • 是的,如上所述创建了控件。如何添加该命名空间?已经有 x:Class="projectname.MainPage" 。我在 mainpage.cs 页面中复制了以上两个类
  • 在我的示例中,控件是在 Controls 命名空间中定义的......所以在我的 xaml 中,我有以下内容:xmlns:controls="using:****"
  • 但我现在看到,也许我最好为你的情况更改示例......你只需要 1 个类。因此,将公共部分类 AlternatingListView : ListView 更改为公共类 AlternatingListView : ListView 并将所有类代码复制粘贴到这 1 个类中。 (因为项目中的一些UI原因,我分开了)
猜你喜欢
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多