This is an old post and I liked D'Hags answere. Unfortunatley it didn't satisfy my reqiurement entirely. Hence I extended D'Hags solution a bit which allows now:
column spans are possible in an arbitrary manner and not only the entire row
column span is defined by using object references. When column 2 and column 3 are databinding to the same object reference then a span is rendered
the column span should behave exaclty like the others columns, so that for instance selections are possible.
The solution goes like this:
A Datagrid with the a style:
That sets the following class to be used instead of using DataGridCellsPanel directly:
public class DataGridSpannedCellPanel : DataGridCellsPanel
{
protected override Size ArrangeOverride(Size arrangeSize)
{
if (DataContext is IEnumerable)
{
base.ArrangeOverride(arrangeSize);
IEnumerable
And it used in the following way:
public partial class MainWindow : Window
{
void AddColumn(DataGrid dg, int i)
{
var col = new DataGridTextColumn();
col.Header = (char)('A' + i);
col.Binding = new Binding("[" + i + "]");
dg.Columns.Add(col);
}
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 10; ++i)
{
AddColumn(dataGrid, i);
}
List rows = new List();
String[] txtHeader = new string[7];
String multiSpan = "MergedColumn";
txtHeader[0] = "Col1";
txtHeader[1] = "Col2";
txtHeader[2] = "Col3";
// these columns should be merged to one, which is indicated by assigning
// the same reference to all columns.
txtHeader[3] = multiSpan;
txtHeader[4] = multiSpan;
txtHeader[5] = multiSpan;
int[] intArr = new int[10];
for (int i = 0; i < 10; i++)
{
intArr[i] = i;
}
rows.Add(txtHeader);
rows.Add(intArr);
dataGrid.ItemsSource = rows;
}
}
}