WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一)

我是把博文当记事本的地方……

WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一)

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

前台XAML代码:

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">




CanUserDeleteRows="False" CanUserAddRows="False"
AutoGenerateColumns="False" IsReadOnly="True"
Background="White" GridLinesVisibility="All" Margin="5"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
HeadersVisibility="Column" AlternationCount="2">








HorizontalAlignment="Center" Foreground="{Binding IA,Converter={StaticResource MyColorConvert}}"/>








HorizontalAlignment="Center" Foreground="{Binding IB,Converter={StaticResource MyColorConvert}}"/>









HorizontalAlignment="Center" Foreground="{Binding IC,Converter={StaticResource MyColorConvert}}"/>




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





然后是列的单元格模板:



HorizontalAlignment="Center" Foreground="{Binding IA,Converter={StaticResource MyColorConvert}}"/>

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

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

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

后台代码:

namespace WpfDataGridCellColors
{
///


/// MainWindow.xaml 的交互逻辑
///

public partial class MainWindow : Window
{
private List _ContrastList;
public List ContrastList
{
get { return _ContrastList; }
set
{
_ContrastList = value;
}
}

public MainWindow()
{
InitializeComponent();

ContrastList = new List();

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 "";
}
}
}

转载于:https://www.cnblogs.com/chaserpower/archive/2012/11/28/2792094.html

你可能感兴趣的:(WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一))