(转载)WPF:DataGrid设置行、单元格的前景色

时间:2022-10-08 05:27:41

0. 说明

 /*********************************
* 本示例实现功能
1.DataGrid基本操作
2.列标题样式
3.内容居中
2.根据条件设置最后一列单元格前景色
3.根据条件设置行前景色
5.自定义分隔线
***********************************/

1. XAML:

<Window x:Class="Summary.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="600">
    <Grid>
  <DataGrid Name="dgTasks" AutoGenerateColumns="False" Margin="10" 
      SelectionMode="Single" ItemsSource="{Binding}" 
      IsReadOnly="True" HeadersVisibility="Column" GridLinesVisibility="None">
   <DataGrid.Resources>
    <!--设置列标题样式-->
    <Style TargetType="DataGridColumnHeader" x:Key="stlColumnHeader">
     <Setter Property="Height" Value="38"/>
     <Setter Property="FontSize" Value="14"/>
     <Setter Property="FontWeight" Value="Bold"/>
     <Setter Property="HorizontalContentAlignment" Value="Center"/>
     <Setter Property="BorderBrush" Value="#bbbbbb"/>
     <Setter Property="BorderThickness" Value="0 0 1 0"/>
    </Style>
    <!--设置DataGrid内容居中-->
    <Style TargetType="TextBlock" x:Key="stlContent">
     <Setter Property="HorizontalAlignment" Value="Center" />
     <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    <!--设置DataGrid内容样式及行的前景色-->
    <Style TargetType="DataGridRow">
     <Setter Property="Height" Value="38"/>
     <Setter Property="FontSize" Value="14"/>
     <Setter Property="Foreground" Value="{Binding RowForeground}"/>
    </Style>
    <!--设置DataGrid单元格样式:分隔线,选中变色[最后一个单元格有自己单独的样式]-->
    <Style TargetType="DataGridCell">
     <Setter Property="BorderBrush" Value="#bbbbbb"/>
     <Setter Property="BorderThickness" Value="0 0 1 1"/>
     <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
       <Setter Property="Background" Value="#ffc343"/>
       <Setter Property="Foreground" Value="#515151"/>
      </Trigger>
     </Style.Triggers>
    </Style>
   </DataGrid.Resources>
   <DataGrid.Columns>
    <DataGridTextColumn Header="任务编号" Binding="{Binding TaskID}" 
         Width="120*" HeaderStyle="{StaticResource stlColumnHeader}"
         ElementStyle="{StaticResource stlContent}"/>
    <DataGridTextColumn Header="任务名称" Binding="{Binding Name}"
            Width="120*" HeaderStyle="{StaticResource stlColumnHeader}"
         ElementStyle="{StaticResource stlContent}"/>
    <DataGridTextColumn Header="任务描述" Binding="{Binding TaskDesc}"
         Width="250*" HeaderStyle="{StaticResource stlColumnHeader}"
         ElementStyle="{StaticResource stlContent}"/>
    <DataGridTextColumn Header="任务状态" Binding="{Binding State}"
         Width="100" HeaderStyle="{StaticResource stlColumnHeader}"
         ElementStyle="{StaticResource stlContent}">
     <DataGridTextColumn.CellStyle>
      <Style TargetType="DataGridCell">
       <Setter Property="Foreground" Value="{Binding CellForeground}"/>
       <Setter Property="BorderBrush" Value="#bbbbbb"/>
       <Setter Property="BorderThickness" Value="0 0 1 1"/>
       <Style.Triggers>
        <Trigger Property="IsSelected" Value="true">
         <Setter Property="Background" Value="#ffc343"/>
         <Setter Property="BorderBrush" Value="#ffc343"/>
        </Trigger>
       </Style.Triggers>
      </Style>
     </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
        
   </DataGrid.Columns>
  </DataGrid>
 </Grid>
</Window>
2. CODE

 

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

namespace Summary
{

    public partial class MainWindow : Window
    {
        private List<Task> tasks = new List<Task>();

        public MainWindow()
        {
            InitializeComponent();
            this.InitializeDataGrid();
        }

        /// <summary>
        /// 获取任务列表
        /// </summary>
        private void InitializeDataGrid()
        {
            Task task = new Task();
            task.TaskID = "T0001";
            task.Name = "任务一";
            task.TaskDesc = "任务一未按时完成";
            task.State = TaskState.未完成;
            tasks.Add(task);

            task = new Task();
            task.TaskID = "T0002";
            task.Name = "任务二";
            task.TaskDesc = "任务二按时完成";
            task.State = TaskState.已完成;
            tasks.Add(task);

            task = new Task();
            task.TaskID = "T0003";
            task.Name = "任务三";
            task.TaskDesc = "任务三超额完成";
            task.State = TaskState.超额完成;
            tasks.Add(task);

            task = new Task();
            task.TaskID = "T0004";
            task.Name = "任务四";
            task.TaskDesc = "任务四夭折";
            task.State = TaskState.夭折;
            tasks.Add(task);

            task = new Task();
            task.TaskID = "T0005";
            task.Name = "任务五";
            task.TaskDesc = "任务五夭折";
            task.State = TaskState.夭折;
            tasks.Add(task);
            //设置与单元格及行相关的前景色
            this.SetRelativeForeground(ref tasks);

            dgTasks.DataContext = tasks;
        }

        /// <summary>
        /// 设置与前景色相关的属性
        /// </summary>
        /// <param name="tasks"></param>
        private void SetRelativeForeground(ref List<Task> tasks)
        {
            foreach (Task task in tasks)
            {
                task.RowForeground = "#515151";
                switch (task.State)
                {
                    case TaskState.未完成:
                        task.CellForeground = "#1e008d";
                        break;
                    case TaskState.已完成:
                        task.CellForeground = "#009864";
                        break;
                    case TaskState.超额完成:
                        task.CellForeground = "#0050a2";
                        break;
                    case TaskState.夭折:
                        task.CellForeground = "#979797";
                        task.RowForeground = "#979797";
                        break;
                }
            }
        }
    }
}