Wpf – Alternate background color of ListView.
Topic: Wpf – Alternate background color of ListView.
标题: wpf – ListView交替背景色
总的来说有三种变换背景色的方法,他们是
· 定义一个IValueConverter的Style
· 扩展ListView ,重载PrepareContainerOverride方法
· 使用 StyleSelector
In general there are three ways to alternate the background color of a ListView.
They are
· Define a style that uses an IValueConverter to Alternate Background color
· Derive a New class from ListView to Alternate Background color
· Use a StyleSelector to Alternate Background color
首先看一看ValueConverter,代码如下:
First we need to introduce the ValueConverter, the code as below.
public sealed class BackgroundConverter : IValueConverter { #region IValueConverter public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ListViewItem item = (ListViewItem) value; ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView; // Use the ItemsControl.ItemsContainerFromItemContainer(item) to get the ItemsControl.. and cast // Get the index of a ListViewItem int index = listView.ItemContainerGenerator.IndexFromContainer(item); // this is a state-of-art way to get the index of an Item from a ItemsControl if (index % 2 == 0) return Brushes.LightBlue; else { return Brushes.Beige; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion IValueConverter }
它使用了ListView.ItemsControlFromItemContainer(item)拿到Item 的Container,然后, 调用ListView.ItemContainerGenerator.IndexFromContainer(item);得到Container的index;
这样使用该Style.
Basically, it uses the ListView.ItemsControlFromItemContainer(item) to get the container for an single Item, and then with the Container that it returns, it can get an index from the ListView – the method to call is ListView.ItemContainerGenerator.IndexFromContainer(item);
You will need to apply the Style somewhere.
<namespc:BackgroundConverter xmlns:namespc="clr-namespace:AlternativeBackgroundListView.Converters" x:Key="myConverter" /> <Style x:Key="myItemStyle" TargetType="{x:Type ListViewItem}"> <Setter Property="Background"> <Setter.Value> <Binding RelativeSource="{RelativeSource Self}" Converter="{StaticResource myConverter}" /> </Setter.Value> </Setter> </Style>
在ListView上使用该style
And you will need to apply that style on the ListView.
<!-- Set Style on the IemContainer style The key is the "ItemContainerStyle --> <ListView x:Name="ListView" ItemsSource="{Binding Customers}" ItemContainerStyle="{StaticResource myItemStyle}" Grid.Row="0" > <ListView.Resources> </ListView.Resources> <ListView.View> <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Some Tooltip" > <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" Width="Auto" > </GridViewColumn> </GridView> </ListView.View> </ListView>
扩展一个ListView
Derive a new class from ListView to alternate the background of a color.
首先看以下SubListView 类
We first introduce the sub listview that we wrote.
public sealed class SubListView : ListView { protected override void PrepareContainerForItemOverride(System.Windows.DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); if (View is GridView) { int index = ItemContainerGenerator.IndexFromContainer(element); // The ItemContainerGenerator has method to get index for a given Item ListViewItem lvi = element as ListViewItem; if (index%2 == 0) lvi.Background = Brushes.LightBlue; else { lvi.Background = Brushes.Beige; } } } }
关键在于ItemContainerGenerator.IndexFromContainer(哪里见过呢),要重载的方法是PreareContianerOverride().
The key here is the ItemContainerGenerator.IndexFromContainer (sounds familiar)?? And the method that we override is PreareContianerOverride()
使用SubListView, xaml如下
To use the SubListview, you can use this in the xaml file.
<!-- the PrepareContainerForItemOverride is the key to the SubListView --> <namespc:SubListView xmlns:namespc="clr-namespace:AlternativeBackgroundListView.Controls" x:Name="SubListView" ItemsSource="{Binding Customers}" Grid.Row="1" > <namespc:SubListView.View> <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Some Tooltip" > <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" Width="Auto" > </GridViewColumn> </GridView> </namespc:SubListView.View> </namespc:SubListView>
使用Style selector
Use of a Style Selector
关键实在ListView. ItemContainerStyleSelector,首先看实现。
The use of style selector uses on the ListView’s ItemContainerStyleSelector. First let’s see the ListViewItemStyleSelector
public class ListViewItemStyleSelector : StyleSelector { public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container) { Style st = new Style(); st.TargetType = typeof (ListViewItem); Setter backgroundSetter = new Setter(); backgroundSetter.Property = ListViewItem.BackgroundProperty; ListView listView = ItemsControl.ItemsControlFromItemContainer(container) as ListView; int index = listView.ItemContainerGenerator.IndexFromContainer(container); if (index%2 == 0) backgroundSetter.Value = Brushes.LightBlue; else { backgroundSetter.Value = Brushes.Beige; } st.Setters.Add(backgroundSetter); return st; } }
使用Style selector,我们希望作到如下
To use the StyleSelector, here is what you do
<!-- THe key is ItemContainerStyleSelector --> <ListView x:Name="ListViewWithStyleSelector" ItemsSource="{Binding Customers}" Grid.Row="2" ItemContainerStyleSelector="{DynamicResource myStyleSelector}" > <ListView.Resources> </ListView.Resources> <ListView.View> <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Some Tooltip" > <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="Auto" ></GridViewColumn> <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" Width="Auto" > </GridViewColumn> </GridView> </ListView.View> </ListView>
数据和DataContext
Data and DataContex
public class Customer { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } }
public class MainWindowViewModel { #region Fields private List<Customer> _customers; #endregion Fields #region Contructors public MainWindowViewModel() { Initialize(); } #endregion Constructors #region Methods public void Initialize() { _customers = new List<Customer>() { new Customer() { Name = "Joe", Address = "Hanhua", Age = 12, }, new Customer() { Name = "Nora", Address = "Hanhua", Age = 32, }, new Customer() { Name = "Karl", Address = "Huaihai", Age = 12, }, new Customer() { Name = "Summer", Address = "Huaihai", Age = 24, }, }; } #endregion Methods public CollectionView Customers { get { return new CollectionView(_customers); } }
References:
How to : Alternate the Background Color for rows in a ListView. http://msdn.microsoft.com/en-us/library/ms750769(v=vs.85).aspx
相关推荐
总结来说,通过自定义WPF ListView的ControlTemplate,我们可以轻松地改变选择和鼠标悬停时的行背景颜色。结合MVVM模式,我们可以将这些颜色设置逻辑移到ViewModel中,保持界面和业务逻辑的分离,同时实现灵活的颜色...
在WPF中,ListView控件是一个非常常用的组件,它用于展示数据集合,并提供了多种布局方式。自定义布局允许开发者根据需求灵活设计ListView显示的内容和样式。本实例将深入讲解如何在WPF中自定义ListView控件的布局。...
wpf修改ListView和ListBox风格,介绍了在wpf中ListView和ListBox的使用
在Windows Presentation Foundation (WPF) 中,ListView是一个强大的控件,可以用来展示各种类型的数据集合。在实际的应用场景中,我们经常需要在ListView中添加可选的复选框(CheckBox)列,以便用户能够选择一个或...
4. **样式和模板**:根据需求自定义ListView的外观,如边框、背景色、选中状态等。可以使用ControlTemplate和Style来实现。 5. **交互行为**:考虑用户如何与横向的ListView进行交互,例如添加点击事件处理、拖放...
在Windows Presentation Foundation (WPF) 中,`ListView`控件是一种强大的数据展示工具,它可以用于显示各种类型的数据,并且能够进行灵活的定制和操作。在本教程中,我们将深入探讨如何实现`ListView`的自动排序...
在Windows Presentation Foundation (WPF) 中,ListView是一个强大的控件,用于展示数据集,并提供了多种方式来呈现和操作数据。本教程将详细讲解如何在WPF应用中使用ListView来显示列表,以及添加删除和保存功能。 ...
可以通过定义ListView.ItemContainerStyle来控制列表项的样式,比如更改选中时的背景色或边框: ```xml <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> ... </ListView....
在WPF中,可以通过DataTrigger或DataTemplateSelector来根据数据项的属性值动态改变ListView项的背景色或前景色。例如,当数据项的状态字段为"警告"时,项背景色可能变为黄色。 接着,`数据绑定`是WPF的核心特性,...
C#+WPF 支持模仿Tab+控件区域的设计和动态切换 支持TreeView动态修改数据 支持ListView动态修改数据,选定行变色,单个cell规则,支持Cell按钮, 支持xml解析,支持登录界面权限控制, 三层树形数据提供给TreeView ...
在WPF(Windows Presentation Foundation)开发中,ListView是一种常用的控件,用于展示数据集合。而ComboBox则是一个下拉选择框,通常用于提供多个选项让用户选择。在某些场景下,我们可能需要在ListView的每一行中...
在Windows Presentation Foundation (WPF) 中,ListView是一个强大的控件,用于展示数据集合,并提供了多种自定义和格式化数据的方式。本教程将深入探讨如何在WPF中实现ListView的可编辑数据项功能。 首先,我们...
- 通过设置ListView的AlternationCount属性,可以实现行交替的背景色,提高可读性。 - 使用ItemContainerStyle来控制ListViewItem的样式,如鼠标悬停时的高亮效果。 5. **性能优化**: - 考虑使用...
本文将详细介绍如何自定义`ListView`和`ListBox`的背景样式,包括但不限于选中行样式、背景颜色、边框样式等。 #### 二、基础知识 在深入探讨之前,我们先了解一些基本概念: 1. **`ListView`** 和 **`ListBox`**...
WPF ListView 自动调整列宽,. 根据父窗体的宽度自动调整每列宽。I want to give * width to my ListView.GridViewColumn, but whenever I am providing * width to ListView.GridViewColumn, it gives me a compile ...
Page1的ListView是别人的代码。MainWindow是我在这个基础上改的。控制数据列表显示隐藏的checkbox是竖排显示,我改了横排显示。MedColumnObject用的, //GridViewColumn集合 ObservableCollection<DataGridColumn> ...
在Windows Presentation Foundation (WPF) 中,ListView 是一个强大的数据展示控件,它允许开发者以多种视图形式显示数据,并提供了丰富的自定义能力。在实际应用中,我们常常需要为ListView添加排序功能,以便用户...
在Windows Presentation Foundation (WPF) 中,ListView 是一个强大的数据展示控件,它可以用来显示各种类型的数据,并且可以通过自定义模板来实现丰富的视图样式。本主题将深入探讨如何在WPF的ListView中实现分页...
在Windows Presentation Foundation (WPF) 中,ListView 是一个强大的数据展示控件,它允许我们以网格形式显示数据,类似于Windows中的资源管理器。然而,在实际应用中,我们可能会遇到一个常见的问题,即如何设置...