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

时间:2020-12-08 19:25:32

与上一篇 WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一) 里的后台实现方法一样,改变前台XAML代码:

前台XAML代码:

<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 CanUserDeleteRows="False" CanUserAddRows="False" 
                  AutoGenerateColumns="False" IsReadOnly="True"                 
                  Name="dataGrid"  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 AFColor}"/>

                        </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 BFColor}"/>

                        </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 CFColor}"/>

                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>

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

这里列单元格模版里的Foreground直接绑定后台的Color属性,要将其变为什么颜色,可在后台代码里进行控制,然后直接绑定到前台代码中。

如下面后台代码中所示的:

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

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 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.Red,
                    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; }
        }
    }
}