如何更改DataGrid中某一行的背景颜色
How to change the background color of a certain row in a DataGrid
例如,在 xaml 中,我有一个名为 PersonList 的数据网格:
<DataGrid Name="PersonList" />
在代码隐藏中我有一个 Person 的集合:
ObservableCollection<Person> persons = ViewModel.PersonModel;
然后我创建了一个Person DataTable,并通过以下方式将其绑定到PersonList:
PersonDataTable.Columns.Add("Name", typeof(string));
PersonDataTable.Columns.Add("Age", typeof(int));
foreach (var person in persons)
{
if (person != null)
{
PersonDataTable.Rows.Add(
Person.Name,
Person.Age
);
}
}
PersonList.ItemSource = PersonDataTable.AsDataView;
我的问题是,如何改变某一行的背景颜色?例如,更改人物年龄 > 50
行的背景颜色
我试图通过访问 PersonList.ItemSource 中的每一行来做到这一点,但我失败了并且该行始终为空:
int count = 0;
foreach (var person in PersonList.ItemSource)
{
var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
if (PersonDataTable.Rows[count].Field<int>(1) > 50)
{
row.Background = Brushes.Gray;
}
count++;
}
请各位WPF高手帮忙:)
你快到了。请尝试以下操作:
int count = 0;
foreach (var person in PersonList.ItemSource)
{
var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
if (PersonDataTable.Rows[count].Field<int>(1) > 50)
{
row.DefaultCellStyle.BackColor = Color.Gray;
}
count++;
}
使用转换器尝试您的逻辑,如下所示:
这是我的 AgeAboveLimitConverter 文件:
using System;
using System.Windows.Data;
namespace DataGridSample.Converter
{
public class AgeAboveLimitConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
return (int)value > 50;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
然后在您的数据网格 xaml 文件中,
添加命名空间 xmlns:converter="clr-namespace:DataGridSample.Converter"
在 DataGrid 中为 DataGridRow 添加样式,
<Grid>
<Grid.Resources>
<converter:AgeAboveLimitConverter x:Key="AgeConverter"/>
</Grid.Resources>
<DataGrid Name="PersonList">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="Background" Value="Transparent" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Age,Converter={StaticResource AgeConverter}}" Value="true">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
例如,在 xaml 中,我有一个名为 PersonList 的数据网格:
<DataGrid Name="PersonList" />
在代码隐藏中我有一个 Person 的集合:
ObservableCollection<Person> persons = ViewModel.PersonModel;
然后我创建了一个Person DataTable,并通过以下方式将其绑定到PersonList:
PersonDataTable.Columns.Add("Name", typeof(string));
PersonDataTable.Columns.Add("Age", typeof(int));
foreach (var person in persons)
{
if (person != null)
{
PersonDataTable.Rows.Add(
Person.Name,
Person.Age
);
}
}
PersonList.ItemSource = PersonDataTable.AsDataView;
我的问题是,如何改变某一行的背景颜色?例如,更改人物年龄 > 50
行的背景颜色我试图通过访问 PersonList.ItemSource 中的每一行来做到这一点,但我失败了并且该行始终为空:
int count = 0;
foreach (var person in PersonList.ItemSource)
{
var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
if (PersonDataTable.Rows[count].Field<int>(1) > 50)
{
row.Background = Brushes.Gray;
}
count++;
}
请各位WPF高手帮忙:)
你快到了。请尝试以下操作:
int count = 0;
foreach (var person in PersonList.ItemSource)
{
var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
if (PersonDataTable.Rows[count].Field<int>(1) > 50)
{
row.DefaultCellStyle.BackColor = Color.Gray;
}
count++;
}
使用转换器尝试您的逻辑,如下所示:
这是我的 AgeAboveLimitConverter 文件:
using System;
using System.Windows.Data;
namespace DataGridSample.Converter
{
public class AgeAboveLimitConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
return (int)value > 50;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
然后在您的数据网格 xaml 文件中,
添加命名空间 xmlns:converter="clr-namespace:DataGridSample.Converter"
在 DataGrid 中为 DataGridRow 添加样式,
<Grid>
<Grid.Resources>
<converter:AgeAboveLimitConverter x:Key="AgeConverter"/>
</Grid.Resources>
<DataGrid Name="PersonList">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="Background" Value="Transparent" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Age,Converter={StaticResource AgeConverter}}" Value="true">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>