临时记载--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);
posted @ 2011-12-29 17:33  西天之旅  阅读(505)  评论(0编辑  收藏  举报