使用ItemsControl ActualHeight对打印的XAML表单进行分页?

时间:2013-01-09 16:23:42

标签: wpf xaml itemscontrol measure actualheight

我需要使用XAML生成打印表单,该表单具有标题网格和可变数量的行,随着行数的增加,这些行可能会导致多个页面。标题必须出现在每个页面上,并且由于行内的文本换行,每行的高度可能不同。我目前正在尝试使用 ItemsControl (行容器)的 ActualHeight 来确定何时生成新页面,但 ActualHeight的值始终为零

我的“XAML_Form”具有以下结构。在ItemTemplate中使用网格,以允许将行中的列与标题网格中的列对齐。

<Grid Width="980" Height="757">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Name="_headerControl" Grid.Row="0"/>
        <ItemsControl Name=_rowsControl ItemsSource={Binding Rows} Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Grid>

我们的架构有一个报表类,可以处理添加页脚,页码以及将页面聚合为PDF。此报告类为每个XAML视图页面都有一个嵌套的ViewModel。每个页面的ViewModel使用支持行对象列表:

   List<RowClass> RowsList;

ViewModel还有一个ICollectionView,用于绑定ItemsSource:

   ICollectionView Rows = new ListCollectionView(RowsList);

我的报告类有一个CreatePages方法,其中包含如下代码:

IList<XAML_Form> pages = new List<XAML_Form>();
var vm = new PageViewModelClass();
var page = new XAML_Form { DataContext = vm };
page.InitializeComponent();
page.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity);
page.Arrange(new Rect(new Point(0,0), page.DesiredSize));
var maxRowsHeight = page.DesiredSize.Height - page._headerControl.ActualHeight;
pages.Add(page);
var rowsOnPage = 0;
foreach (var row in sourceRowsObjectList)
{
    rowsOnPage++;
    vm.RowsList.Add(row);
    vm.Rows.Refresh();
    page.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity);
    page.Arrange(new Rect(new Point(0,0), page.DesiredSize));
    if (page._rowsControl.ActualHeight <= maxRowsHeight)
        continue;

    // The rows exceed the available space; the row needs to go on the next page.
    vm.RowsList.RemoveAt(--rowsOnPage);
    vm = new PageViewModelClass();
    vm.RowsList.Add(row);
    rowsOnPage = 1;
    page = new XAML_Form { DataContext = vm };
    page.InitializeComponent();
    pages.Add(page);
}
return pages;

初始测量/排列确实为我提供了maxRowsHeight的预期值。并且生成的表单对于具有几行的单个页面看起来很好。我的具体问题是:为什么page._rowsControl.ActualHeight始终为零?通常,有没有更好的方法解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是一个解决方案。我们正在尝试将视图问题与视图模型问题分开,因此仍有待改进。

报告类中的CreatePages方法现在是:

private static IEnumerable<XAML_Form> CreatePages()
{
    IList<XAML_Form> pages = new List<XAML_Form>();
    int rowCount = sourceRowsObjectList.Count;
    int remainingRowCount = rowCount;
    do
    {
        var pageVm = new PageViewModelClass();
        var page = new XAML_Form(pageVm);
        pages.Add(page);

        int numberOfRowsToAdd = Math.Min(remainingRowCount, XAML_Form.MaxNumberOfRows);
        pageVm.AddRows(sourceRowsObjectList.Skip(rowCount - remainingRowCount).Take(numberOfRowsToAdd));
        remainingRowCount -= numberOfRowsToAdd;
        while (page.AreRowsOverflowing())
        {
            pageVm.RemoveLastRow();
            remainingRowCount++;
        }
    } while (remainingRowCount > 0);
    return pages;
}

背后的相关XAML_Form代码如下:

private static int _maxNumberOfRows = -1;

public XAML_Form(PageViewModelClass viewModel)
{
    InitializeComponent();
    Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
    Arrange(new Rect(new Point(0, 0), DesiredSize);
    ViewModel = viewModel;
}

public PageViewModelClass ViewModel
{
    get { return (PageViewModelClass)DataContext; }
    private set { DataContext = value; }
}

public static int MaxNumberOfRows
{
    get     // Compute this only once, the first time it is called.
    {
        if (_maxNumberOfRows < 0) return _maxNumberOfRows;
        var page = new XAML_Form();
        var singleRowCollection = new object[] { null; }
        page._rowsControl.ItemsSource = singleItemCollection;
        page._rowsControl.UpdateLayout();
        var rowHeight = page._rowsControl.ActualHeight;
        _maxNumberOfRows = (int)((page.DesiredSize.Height - page._headerControl.ActualHeight) / rowHeight);
        page._rowsControl.ItemsSource = null;
        return _maxNumberOfRows;
    }
}

// Call this method as rarely as possible. UpdateLayout is EXPENSIVE!
public bool AreRowsOverflowing()
{
    _rowsControl.UpdateLayout();
    return _rowsControl.ActualHeight > DesiredSize.Height - _headerControl.ActualHeight;
}