sorry,对这种需求我不会费事使用datagrid,而是直接画<Page x:Class="WpfBrowserApplication1.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" xmlns:local="clr-namespace:WpfBrowserApplication1" d:DesignHeight="700" d:DesignWidth="1000" Title="Page1"> </Page> using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Data;namespace WpfBrowserApplication1 { /// <summary> /// Page1.xaml 的交互逻辑 /// </summary> public partial class Page1 : Page { DrawingVisual dv = new DrawingVisual(); DataTable dt = new DataTable(); public Page1() { this.VisualEdgeMode = EdgeMode.Aliased; dt.Columns.Add("A", typeof(int)); dt.Columns.Add("B", typeof(DateTime)); for (int i = 0; i < 60; i++) { DataRow dr = dt.NewRow(); dr[0] = i; dr[1] = DateTime.Now.AddDays(-14).AddDays(i % 31); dt.Rows.Add(dr); } dt.AcceptChanges();
InitializeComponent(); Pen blackp = new Pen(Brushes.Black, 1); System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("zh-cn"); FlowDirection fd = FlowDirection.LeftToRight; Typeface tf = new Typeface("宋体"); double d = 12; using (DrawingContext dc = dv.RenderOpen()) { for (int i = 0; i < 5; i++) { dc.DrawRectangle(null, blackp, new Rect(217 * i, 0, 217, 30)); dc.DrawText(new FormattedText("第" + (i + 1).ToString() + "周", ci, fd, tf, d, Brushes.Black), new Point(217 * i + 70, 10)); } for (int i = 0; i < 35; i++) dc.DrawRectangle(null, blackp, new Rect(31 * i, 30, 31, 30)); int w = new DateTime(2011, 3, 1).DayOfWeek.GetHashCode(); for(int i = w;i<w + 31;i++) dc.DrawText(new FormattedText((i - w + 1).ToString(), ci, fd, tf, d, Brushes.Black), new Point(31 * i + 5, 40)); for (int i = 0; i < 35; i++) dc.DrawRectangle(null, blackp, new Rect(31 * i, 60, 31, 60)); for (int i = 0; i < dt.Rows.Count; i++) { int week = ((DateTime)dt.Rows[i][1]).DayOfWeek.GetHashCode(); int day = ((DateTime)dt.Rows[i][1]).Day; string s = dt.Rows[i][0].ToString(); double left = (double)(week + (day + week / 7 * 7 + 1) * 31 + 5); double top = i > 30 ? 100 : 70; dc.DrawText(new FormattedText(s, ci, fd, tf, d, Brushes.Black), new Point(left, top)); } } } protected override int VisualChildrenCount { get { return 1; } } protected override Visual GetVisualChild(int index) { return dv; } } }
置合并列头或者单元格,数据行不改变。
谢谢!
就是个datagrid
1、生成新的数据源,也就是查询结果的数据源,数据源里面先实现合并等内容,然后绑定到标准datagrid。
2、还是原来的数据源,datagrid重新定义列头,然后显示时通过Converter来绑定合并后的数据。
3、使用DrawingVisual类来画,这个你可以搜索一下msdn里面这个类。这种方式最自由,没拘束,效率高。
没有具体需求实在没办法给你做示例程序,不好意思。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:WpfBrowserApplication1"
d:DesignHeight="700" d:DesignWidth="1000"
Title="Page1">
</Page>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Data;namespace WpfBrowserApplication1
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
DrawingVisual dv = new DrawingVisual();
DataTable dt = new DataTable();
public Page1()
{
this.VisualEdgeMode = EdgeMode.Aliased;
dt.Columns.Add("A", typeof(int));
dt.Columns.Add("B", typeof(DateTime));
for (int i = 0; i < 60; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = DateTime.Now.AddDays(-14).AddDays(i % 31);
dt.Rows.Add(dr);
}
dt.AcceptChanges();
InitializeComponent();
Pen blackp = new Pen(Brushes.Black, 1);
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("zh-cn");
FlowDirection fd = FlowDirection.LeftToRight;
Typeface tf = new Typeface("宋体");
double d = 12;
using (DrawingContext dc = dv.RenderOpen())
{
for (int i = 0; i < 5; i++)
{
dc.DrawRectangle(null, blackp, new Rect(217 * i, 0, 217, 30));
dc.DrawText(new FormattedText("第" + (i + 1).ToString() + "周", ci, fd, tf, d, Brushes.Black), new Point(217 * i + 70, 10));
}
for (int i = 0; i < 35; i++)
dc.DrawRectangle(null, blackp, new Rect(31 * i, 30, 31, 30));
int w = new DateTime(2011, 3, 1).DayOfWeek.GetHashCode();
for(int i = w;i<w + 31;i++)
dc.DrawText(new FormattedText((i - w + 1).ToString(), ci, fd, tf, d, Brushes.Black), new Point(31 * i + 5, 40));
for (int i = 0; i < 35; i++)
dc.DrawRectangle(null, blackp, new Rect(31 * i, 60, 31, 60));
for (int i = 0; i < dt.Rows.Count; i++)
{
int week = ((DateTime)dt.Rows[i][1]).DayOfWeek.GetHashCode();
int day = ((DateTime)dt.Rows[i][1]).Day;
string s = dt.Rows[i][0].ToString();
double left = (double)(week + (day + week / 7 * 7 + 1) * 31 + 5);
double top = i > 30 ? 100 : 70;
dc.DrawText(new FormattedText(s, ci, fd, tf, d, Brushes.Black), new Point(left, top));
}
}
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
return dv;
}
}
}