(转载)WPF:DataGrid设置行、单元格的前景色
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设置行、单元格的前景色的更多相关文章
- c# WPF DataGrid 获取选中单元格信息
private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- poi excel设置合并单元格边框格式
版本3.17 //设置合并单元格的边框 public static void setBorderForMergeCell(BorderStyle style,int color, CellRangeA ...
- [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标
目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...
- [Xcode 实际操作]五、使用表格-(2)设置UITableView单元格高度
目录:[Swift]Xcode实际操作 本文将演示如何制作一个自定义行高的表格视图 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...
- NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】 NPOI 单元格 格式设为文本 HSSFDataFormat
NPOI2.2.0.0实例详解(十)—设置EXCEL单元格[文本格式] 2015年12月10日 09:55:17 阅读数:3150 using System; using System.Collect ...
- 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...
随机推荐
- 怎么解决sublime的插件自动被禁用
前两天,我的sublime text安装的很多插件都被自动禁用了,每次都要我自己重新启用一下才可以,后来从网上找到了解决方法. 找到“设置”-“Package Settings”-“Package C ...
- Linux安装JDK1.7 prm
[转]Linux安装JDK1.7 prm 一.卸载JDK Linux会自带JDK,如果不使用自带版本的话需要卸载. 1.卸载系统自带的jdk版本 查看自带的jdk #rpm -qa | ...
- FASTQ格式
FASQT格式是用于存储生物序列(通常是核苷酸序列)及其相应的碱基质量分数的一种文本格式.为简洁起见,序列字母和质量分数均使用单个ASCII字符进行编码.最初由Wellcome Trust Sange ...
- 使用Axis2方式发布webService的三种方式
1.Axis2的下载和安装 首先可以下载如下两个zip包:axis2-1.6.1-bin.zipaxis2-1.6.1-war.zip其中 axis2-1.6.1-bin.zip文件中包含了Axis2 ...
- eos表结构总结说明
EOS6.0 WORKFLOW 表结构说明 流程定义表(WFProcessDefine) 名称 代码 描述 流程定义ID processDefID 主键 流程定义名称 processDefName 业 ...
- EF外键保存数据
using (DataContext dbcontext=new DataContext ()) { //emp.department.ID = dep.ID; //emp.department = ...
- Spring Security 表达式(Expressions) - hasRole示例
1.概述 Spring Security使用强大的Spring Expression Language(SpEL)提供各种各样的表达式.大多数这些Security表达式是针对上下文对象(当前经过身份验 ...
- 无监督学习:Neighbor Embedding(邻域嵌套)
一 Manifold Learning 我们要做的是非线性的降维,data是分布在低维空间里面,只是被扭曲到了高维空间. 比如地球的表面是一个二维平面,但是被塞到一个三维空间中. Manifold就是 ...
- xampp搭建discuz论坛
xampp搭建discuz论坛 软件相关 xampp 下载 1.下载xampp,地址 2.下载discuz,地址 配置 1.安装xampp并启动apache和mysql 2.将discuz安装包中的u ...
- unity coroutine
http://gad.qq.com/article/detail/695 使用Unity 3D引擎的同学,对于Coroutine(协程)的使用肯定也是非常熟悉的了.然而Coroutine背后的技术以及 ...