WPF DataGrid 改变单元格字体颜色

2021-12-30 15:56:39 浏览数 (2)

第一种方法,指定 DataGrid 里单元格的值是否符合用户自定义的条件,如果不,改变指定的颜色:

前台XAML代码:

代码语言:javascript复制
<Window x:Class="WpfDataGridCellColors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loca="clr-namespace:WpfDataGridCellColors"
Title="MainWindow" Height="261" Width="649">

<Window.Resources>
<loca:ColorConverter x:Key="MyColorConvert"/>
</Window.Resources>

<Grid>
<DataGrid x:Name="dataGrid"
CanUserDeleteRows="False" CanUserAddRows="False"
AutoGenerateColumns="False" IsReadOnly="True"
Background="White" GridLinesVisibility="All" Margin="5"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
HeadersVisibility="Column" AlternationCount="2">

<DataGrid.Columns>

<DataGridTextColumn Binding="{Binding Title}" Header="项目" Width="150"/>

<DataGridTemplateColumn Width="150" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="A" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IA}" VerticalAlignment="Center"
HorizontalAlignment="Center" Foreground="{Binding IA,Converter={StaticResource MyColorConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

<DataGridTemplateColumn Width="150" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="B"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IB}" VerticalAlignment="Center"
HorizontalAlignment="Center" Foreground="{Binding IB,Converter={StaticResource MyColorConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

<DataGridTemplateColumn Width="150" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="C" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IC}" VerticalAlignment="Center"
HorizontalAlignment="Center" Foreground="{Binding IC,Converter={StaticResource MyColorConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

</DataGrid.Columns>

</DataGrid>
</Grid>
</Window>

这里要注意的是定义DataGrid的列模板,首先是列标题模块:

代码语言:javascript复制
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="A" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

然后是列的单元格模板:

代码语言:javascript复制
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IA}" VerticalAlignment="Center"
HorizontalAlignment="Center" Foreground="{Binding IA,Converter={StaticResource MyColorConvert}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

这里的Foreground绑定后台生成的值 IA ,当IA符合用户定义的一些条件后,改变颜色。

条件判断与颜色转变在下面的IValueConverter类中。

(如果要改变单元格的背景颜色,只需把前台XAML中的Foreground改为Background即可)

后台代码:

代码语言:javascript复制
namespace WpfDataGridCellColors
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private List<Contrasts> _ContrastList;
public List<Contrasts> ContrastList
{
get { return _ContrastList; }
set
{
_ContrastList = value;
}
}

public MainWindow()
{
InitializeComponent();

ContrastList = new List<Contrasts>();

ContrastList.Add(new Contrasts()
{
Title = "减数",
IA = 110,
AFColor = Brushes.Black,
IB = 220,
BFColor = Brushes.Black,
IC = 330,
CFColor = Brushes.Green
});

dataGrid.ItemsSource = ContrastList;
}
}

public class Contrasts
{
private string _Title;
public string Title
{
get { return _Title; }
set { _Title = value; }
}

private double _IA;
public double IA
{
get { return _IA; }
set { _IA = value; }
}

private double _IB;
public double IB
{
get { return _IB; }
set { _IB = value; }
}

private double _IC;
public double IC
{
get { return _IC; }
set { _IC = value; }
}

private SolidColorBrush _AFColor;
public SolidColorBrush AFColor
{
get { return _AFColor; }
set { _AFColor = value; }
}

private SolidColorBrush _BFColor;
public SolidColorBrush BFColor
{
get { return _BFColor; }
set { _BFColor = value; }
}

private SolidColorBrush _CFColor;
public SolidColorBrush CFColor
{
get { return _CFColor; }
set { _CFColor = value; }
}
}
}

IValueConverter类:

using System;
using System.Collections.Generic;
using System.Windows.Data;
using System.Windows;

namespace WpfDataGridCellColors
{
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type typeTarget, object param, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return "Black";
}

if (System.Convert.ToDouble(value) >= 100.0 && System.Convert.ToDouble(value) < 200)
{
return "Black";
}

if (System.Convert.ToDouble(value) >= 200.0 && System.Convert.ToDouble(value) < 300)
{
return "Green";
}

if (System.Convert.ToDouble(value) >= 300.0)
{
return "Red";
}

return "Black";
}
public object ConvertBack(object value, Type typeTarget, object param, System.Globalization.CultureInfo culture)
{
return "";
}
}
}

0 人点赞