So, after we started to do all this, I reached a very pleasant solution.
Itβs best and easiest to apply a data pattern to specific rows after loading a DataGrid. So, I stuck to the original idea with DataTables and remembered the indexes that should have changed their template. I just took DataGridRows from these indexes and applied the template on which the custom ItemsPanelTemplate was created, which spans multiple columns.
EDIT: When prompted by Daniel, I include the code.
First of all, we need to create a template for the string:
<ControlTemplate TargetType='{x:Type DataGridRow}' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' > <Border> <DataGridCellsPresenter Foreground='Black'> <DataGridCellsPresenter.ItemsPanel> <ItemsPanelTemplate> <local:DataGridSpannedCellPanel /> </ItemsPanelTemplate> </DataGridCellsPresenter.ItemsPanel> </DataGridCellsPresenter> </Border> </ControlTemplate>
NOTE. local: DataGridSpannedCellPanel is a custom DataGridCellsPanel with an overridden ArrangeOverride method that makes the entire cell size of the entire cell.
You can make an encoded string and load a template from it, for example. The following is creating your grid and initializing some lines with this new template:
var newGrid = MakeNewDataGrid(); newGrid.ItemsSource = myTable.AsDataView(); var template = XamlReader.Parse(HeaderRowTemplate) as ControlTemplate; foreach (int index in myHeaderIndices) { var container = newGrid.ItemContainerGenerator.ContainerFromIndex(index); var row = container as DataGridRow; if (row != null) { row.Template = template; } }
Also note that the rows in the table should be made as follows:
if (bindableQuote.IsGroup()) { table.Rows.Add("Header"); } else table.Rows.Add(rowData.ToArray());
What about this, it remains only to figure out how to implement the DataGridSpannedCellPanel.
Dejan maksimovic
source share