• 临时记载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);
  • 相关阅读:
    六、TreeSet中的定制排序
    五、Set接口
    四、List接口
    Vocabulary Recitation 2020/05/20
    Vocabulary Recitation 2020/05/19
    Vocabulary Recitation 2020/05/18
    自控力_第六章
    Vocabulary Recitation 2020/05/17
    Vocabulary Recitation 2020/05/15
    Vocabulary Recitation 2020/05/14
  • 原文地址:https://www.cnblogs.com/xtzhilv/p/2306467.html
Copyright © 2020-2023  润新知