C# WPF Listview 如何设置某一行的颜色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# WPF Listview 如何设置某一行的颜色相关的知识,希望对你有一定的参考价值。
比如listview里边包含了姓名,年龄两项,如何将年龄大于90岁的行的所有文字变红
你得自己写个转换器,在绑定模板中将某个属性转换从而返回背景色,即可
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:BGConvert x:Key="bgconvert" ></local:BGConvert>
</Grid.Resources>
<ListView Name="listView" >
<ListView.View>
<GridView >
<GridViewColumn DisplayMemberBinding="Binding Name"
Header="Name" Width="120"/>
<GridViewColumn
Header="Age" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Background="Binding Path=Age,Converter=StaticResource bgconvert ">
<TextBlock Text="Binding Age"></TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
this.Loaded += MainWindow_Loaded;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
List<Person> ps = new List<Person>();
ps.Add(new Person() Name = "Tom", Age = 80 );
ps.Add(new Person() Name = "jack", Age = 91 );
listView.ItemsSource = ps;
public class Person
public string Name get; set;
public int Age get; set;
public class BGConvert : IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if((int)value>90)
return new SolidColorBrush(Colors.Red);
else
return new SolidColorBrush(Colors.White);
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
参考技术A 你在添加listview的时候 ,根据年龄的值来设置每一项的ListViewItem的背景色就可以了,
以上是关于C# WPF Listview 如何设置某一行的颜色的主要内容,如果未能解决你的问题,请参考以下文章
WPF的listview的问题。 如何实现鼠标移动到某一行,该行中的button列的button的属性设为Visible(可见)。