• 临时记载C#中给DataGrid添加甘特图效果


    var dtMin = Convert.ToDateTime(dt.Compute("min(Start)", "1=1"));
    var dtMax = Convert.ToDateTime(dt.Compute("max(Finish)", "1=1"));
    int days = (dtMax - dtMin).Days;
    while (dgMain.Columns.Count > 5)
    dgMain.Columns.RemoveAt(dgMain.Columns.Count - 1);
    for (int i = 0; i <= days; i++)
    {
    string colName = "RectColor" + i;
    var tmpDate = dtMin.AddDays(i).Date;
    string exp = @"iif (Start>='" + tmpDate.AddHours(12) + @"' , '#00ffffff' ,iif ('" + tmpDate + @"'>Finish, '00FFFFFF' , iif(OkDate>'" + tmpDate.AddHours(12) + "' or PercentOfComplete=100,'#FF668866','#FF66cc00') )) ";
    dt.Columns.Add(colName, typeof(string), exp);

    //内存中动态生成一个XAML,描述了一个DataTemplate
    XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    XElement xDataTemplate = new XElement(ns + "DataTemplate", new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"),
    new XElement(ns + "Rectangle", new XAttribute("Fill", @"{Binding " + colName + "}"), new XAttribute("Margin", @"-2,3,-2,0"))); //new XAttribute("Stroke", @"#FFF06600"), new XAttribute("StrokeThickness", @"0,0,0,2")
    //将内存中的XAML实例化成为DataTemplate对象,并赋值给
    //ListBox的ItemTemplate属性,完成数据绑定
    XmlReader xr = xDataTemplate.CreateReader();

    DataTemplate dataTemplate = XamlReader.Load(xr) as DataTemplate;

    DataGridTemplateColumn col = new DataGridTemplateColumn();
    col.CellStyle = this.Resources["GanttCell"] as Style;
    //col.HeaderStyle = this.Resources["GanttCell"] as Style;
    col.CellTemplate = dataTemplate;
    col.Header = tmpDate.ToString("M/d");
    if (tmpDate.Date == DateTime.Today)
    {
    col.Header = "今天";
    SolidColorBrush bb = new SolidColorBrush(Color.FromRgb(238, 238, 238));
    Setter set = new Setter(DataGridCell.BackgroundProperty, bb);
    SetterBaseCollection sets = new SetterBaseCollection();
    Style style = new System.Windows.Style();
    style.Setters.Add(set);
    col.CellStyle = style;
    }
    dgMain.Columns.Add(col);
  • 相关阅读:
    最近遇到了这个坑,特意记录下
    《java8实战阅读笔记》
    Namespace的简讲
    发展历程C++及C++与C语言的关系
    进程间通信的概述2
    本来调试无误的程序在真机运行时报标题错误解决方案
    navagationController 的子控制器如何取消右滑返回
    iOS常见的设计模式
    代码:Masonry 第三方框架
    Autolayout的在storyboard警告和错误
  • 原文地址:https://www.cnblogs.com/xtzhilv/p/2306467.html
Copyright © 2020-2023  润新知