(轉載)WPF:DataGrid設置行、單元格的前景色

0. 說明this

 /*********************************
* 本示例實現功能
1.DataGrid基本操做
2.列標題樣式
3.內容居中
2.根據條件設置最後一列單元格前景色
3.根據條件設置行前景色
5.自定義分隔線
***********************************/spa

1. XAML:xml

<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. CODEget

 

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

namespace Summary
{io

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

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

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

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

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

相關文章
相關標籤/搜索