WPF DataGrid如何根据单元格数值动态修改单元格背景颜色
WPF DataGrid按数值动态设置单元格颜色实现方案
前置修改说明
你当前代码中将double数值提前转为了百分比字符串存储,无法直接做数值比较,首先需要调整数据模型保留原始数值,再通过WPF的值转换器实现颜色动态绑定,具体修改如下:
1. 修改C#后台代码
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Data; using System.Windows.Media; namespace datagrid { public partial class MainWindow : Window { public List<double> ValueList1 = new List<double>(); public MainWindow() { InitializeComponent(); // 模拟数值批量生成,这里保留示例的4个,可自行扩展到1000个 ValueList1.Add(0.32); ValueList1.Add(0.90); ValueList1.Add(0.23); ValueList1.Add(0.88); TestValues john = new TestValues { Test1 = ValueList1[0], Test2 = ValueList1[1], Test3 = ValueList1[2], Test4 = ValueList1[3] }; dataGridView1.Items.Add(john); } public class TestValues { // 保留原始double数值,百分比格式化放到XAML中处理 public double Test1 { get; set; } public double Test2 { get; set; } public double Test3 { get; set; } public double Test4 { get; set; } } } // 自定义值转换器:数值转背景色 public class ValueToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is double num) { return num > 0.85 ? Brushes.Red : Brushes.Green; } return Brushes.Transparent; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
2. 修改XAML前端代码
<Window x:Class="datagrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:datagrid" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <!-- 引入转换器资源 --> <Window.Resources> <local:ValueToColorConverter x:Key="ValueToColorConverter"/> <!-- 定义单元格样式 --> <Style x:Key="ColorCellStyle" TargetType="DataGridCell"> <Setter Property="Background" Value="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}, Converter={StaticResource ValueToColorConverter}}"/> <!-- 可选:设置白色文字方便深色背景下查看 --> <Setter Property="Foreground" Value="White"/> </Style> </Window.Resources> <Grid> <DataGrid Height="243" Margin="142,77,213,99" x:Name="dataGridView1" Background="Black" AutoGenerateColumns="False" > <DataGrid.Columns> <!-- 绑定时加StringFormat=P0实现百分比显示,同时引用样式 --> <DataGridTextColumn Header="Test1" Width="100" Binding="{Binding Test1, StringFormat=P0}" CellStyle="{StaticResource ColorCellStyle}" /> <DataGridTextColumn Header="Test2" Width="100" Binding="{Binding Test2, StringFormat=P0}" CellStyle="{StaticResource ColorCellStyle}" /> <DataGridTextColumn Header="Test3" Width="100" Binding="{Binding Test3, StringFormat=P0}" CellStyle="{StaticResource ColorCellStyle}" /> <DataGridTextColumn Header="Test4" Width="100" Binding="{Binding Test4, StringFormat=P0}" CellStyle="{StaticResource ColorCellStyle}" /> </DataGrid.Columns> </DataGrid> </Grid> </Window>
批量处理1000个数值的优化建议
如果有大量数值需要展示,不需要手动逐个定义Test1、Test2属性,可通过DataTable动态生成列,再批量绑定数据即可,无需重复修改前端XAML的列定义。
内容的提问来源于stack exchange,提问作者Lukas Klossek
相关产品推荐
相关产品推荐

