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

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

前台XAML代码:

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

    
        "MyColorConvert"/>
    

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

            

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

                "150" >
                    
                        

                            "A" />

                        
                    
                    
                        

                            "{Binding IA}" VerticalAlignment="Center" HorizontalAlignment="Center"
                                               Foreground="{Binding AFColor}"/>

                        
                    
                


                "150" >
                    
                        

                            "B" />

                        
                    
                    
                        

                            "{Binding IB}" VerticalAlignment="Center" HorizontalAlignment="Center"
                                               Foreground="{Binding BFColor}"/>

                        
                    
                

                "150" >
                    
                        

                            "C" />

                        
                    
                    
                        

                            "{Binding IC}" VerticalAlignment="Center" HorizontalAlignment="Center"
                                               Foreground="{Binding CFColor}"/>

                        
                    
                

            

        
    

这里列单元格模版里的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
{
    /// 
    /// 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.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; }
        }
    }
}

 

 

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

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