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

(转载)WPF:DataGrid设置行、单元格的前景色的更多相关文章

  1. c# WPF DataGrid 获取选中单元格信息

    private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...

  2. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  3. WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  4. poi excel设置合并单元格边框格式

    版本3.17 //设置合并单元格的边框 public static void setBorderForMergeCell(BorderStyle style,int color, CellRangeA ...

  5. [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标

    目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...

  6. [Xcode 实际操作]五、使用表格-(2)设置UITableView单元格高度

    目录:[Swift]Xcode实际操作 本文将演示如何制作一个自定义行高的表格视图 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...

  7. NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】 NPOI 单元格 格式设为文本 HSSFDataFormat

    NPOI2.2.0.0实例详解(十)—设置EXCEL单元格[文本格式] 2015年12月10日 09:55:17 阅读数:3150 using System; using System.Collect ...

  8. 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  9. wpf datagrid设置右键菜单打开时选中项的背景色

    原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...

随机推荐

  1. MVVM里绑定TreeView控件的SelectedItem

    <TreeView x:Name="treeView"> <i:Interaction.Triggers> <i:EventTrigger Event ...

  2. TripAdvisor architecture 2011/06

    http://highscalability.com/blog/2011/6/27/tripadvisor-architecture-40m-visitors-200m-dynamic-page-vi ...

  3. day4 函数重载

    函数的重载 1.函数重载的定义:在同一个类中,有一个以上的同名函数,只要函数的参数列表或参数类型不一样即可,与返回值无关, 这些统称为方法的重载. 2.函数的重载存在的原因:为了增强方法的阅读性,优化 ...

  4. WCF部署到IIS上调用报错:由于扩展配置问题而无法提供您请求的页面

    将WCF部署到全新win7 x64 IIS7.5上访问报错:由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 原因:IIS不识别.sv ...

  5. 以交互方式将文本添加到图形中(matlab)

    这篇博客记录一下怎么用matlab在图形中简单的添加一些文本,以直方图均衡化为例.先看几张图片吧,第一幅是较暗的花粉的电子显微图像和对应的直方图,第二幅是其直方图均衡化的图像和对应的直方图,第三幅是对 ...

  6. Ubuntu 16 Mysql 安装配置

    安装Mysql   apt-get update;   apt-get install mysql-server apt-get install mysql-client //安装过程中会提示修改密码 ...

  7. .net过滤器重写beginrequest

    在J2EE Web开发中有过滤器filter,该filter可以对指定的URL访问进行拦截,并执行过滤器的方法,根据实际应用情况,在过滤器中修改请求的代码.判断会话信息,也可以做权限控制,总之这个过滤 ...

  8. 60行代码实现一个迷你版Vue Router

    这是一个超级精简版的VueRouter,实现hash模式下,hash改变组件切换的功能,原理就是利用了 Vue.js 的响应式机制触发router-view组件的重新渲染. 代码 https://gi ...

  9. 硬盘MBR和GPT区别

    似乎人人都可以张嘴就说"我懂电脑",但是总有一些看起来完全不懂但实际上非常基础的东西让"懂"与"不懂"清晰地划清界限.比如UEFI+GPT就 ...

  10. 去掉word文档两边的空白

    1.设置-页面布局-页边距,把左边距和右边距的数据设置到最小就好,一般为0.43CM 2.把WORD页面顶部标尺,左右拉到最底,如图: 3.在打印预览里,设置页边距,操作方法同 上述 1,如图: