• 临时记载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);
  • 相关阅读:
    騎士宣言
    [洛谷P1631] 序列合并
    [HNOI2006]公路修建问题
    [洛谷2068] 统计和
    [洛谷P1168] 中位数
    【模板】可持久化数组(可持久化线段树/平衡树)
    【模板】可持久化线段树 1(主席树)
    [JSOI2008]最大数maxnumber
    NOI导刊2010提高(06) 黑匣子
    [洛谷1533] 可怜的狗狗
  • 原文地址:https://www.cnblogs.com/xtzhilv/p/2306467.html
Copyright © 2020-2023  润新知