使一些 DataGrid 单元格跨越多列 [英] Make some of DataGrid cells span multiple columns

查看:22
本文介绍了使一些 DataGrid 单元格跨越多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经搜索了很长时间来解决这个问题.我正在为 WPF DataGrids 开发简单的打印系统,并设法使用 DataTable 打印具有统一单元格放置的表格并将其设置为 DataGrid 的 ItemSource.

OK, I have searched for pretty long time for solution of this problem. I am developing simple printing system for WPF DataGrids and have managed to print tables with uniform cell placement using DataTable and setting it as DataGrid's ItemSource.

但是,我需要一些行来只包含一个单元格(你可以把它想象成表格中的行组标题").

However, I needed some rows to contain only one cell (you can think of it like "row group header" inside the table).

因此,由于我还没有找到有关跨越多列的 DataTable 单元格的任何信息(如果可以做到,那么知道如何进行将是一件很棒的事情),我想我必须手动向 DataGrid 添加行,然后解决它是这样的:

So, since I haven't found anything about DataTable's cells spanning multiple columns (if this can be made, it would be a great thing to know how), I figured I would have to add rows to DataGrid manually, and solve it something like this:

  • 使用所需的列创建新的 DataGrid
  • 逐行添加,设置跨行或不跨行的DataGridCellPanel

第二点是我有问题的地方(如果是对的,那就是).我需要添加行到使用简单字符串数组作为单元格数据的DataGrid(数组中的索引应该与单元格索引匹配).有没有简单的方法来做这样的事情?

The second point is where I have the problem (if it's right, that is). I need to add row to a DataGrid that uses simple array of strings as cell data (index in array should mach the cell index). Is there an easy way to do something like that?

推荐答案

所以在对这一切进行了更多的摆弄之后,我找到了一个非常好的解决方案.

So after some more fiddling with all this, I have reached a very nice solution.

最好和最简单的做法是在加载 DataGrid 后将数据模板应用于特定行.因此,我坚持使用 DataTables 的原始想法,并记住需要更改其模板的索引.我只是从这些索引中获取 DataGridRows 并应用了具有跨多个列的自定义 ItemsPanelTemplate 的模板.

The best and easiest thing to do is to apply a data template to particular rows after the DataGrid is loaded. So, I stuck to original idea with DataTables and remembered the indices that needed to have their template changed. I just took the DataGridRows from these indices and applied the template that had custom made ItemsPanelTemplate that spans multiple columns.

应丹尼尔的要求,我添加了一些代码.

On Daniel's request, I'm including some code.

我们首先需要一个用于跨行的模板:

First thing we need is a template for spanning row:

<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>

注意:local:DataGridSpannedCellPanel 是一个自定义 DataGridCellsPanel,带有重写的ArrangeOverride 方法,使第一个单元格跨越整个大小.

NOTE: local:DataGridSpannedCellPanel is a custom DataGridCellsPanel with overridden ArrangeOverride method that makes first cell span entire size.

例如,您可以在代码隐藏中创建一个字符串并从中加载您的模板.接下来是创建网格并使用这个新模板初始化一些行:

You can make a string in code-behind and load your template from it, for example. The next thing is creating your grid and initializing some of the rows 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, rows in your table need to be made as follows:

if (bindableQuote.IsGroup())
{
    table.Rows.Add("Header");
}
else
{
    table.Rows.Add(rowData.ToArray());
}

就是这样,剩下的就是弄清楚如何实现 DataGridSpannedCellPanel.

That's about it, the only thing left is to figure out how to implement DataGridSpannedCellPanel.

这篇关于使一些 DataGrid 单元格跨越多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆