我的自定义控件是数独网格。它的行为类似于listbox或combobox之类的任何itemcontrol。物品是细胞。项目绑定到这些单元格的集合。我的控件自动将这些单元格放入网格(9x9板)。
当我用物品放置我的控件时,一切看起来都很好并且细胞存在。
问题是当我尝试使用文档分页器打印我的控件时。打印控件但它是空的,没有任何项目。看起来控件需要以某种方式重新绘制或通知更新自身并加载绑定值。有什么线索吗?
这是我在将其传递给文档分页器之前动态生成我的控件的方法,其中ehich用于在页面上放置多个控件:
private UIElement generateGridControl(SudokuFile file, int row, int column)
{
//Contsruct control
View.GridControl.GridControl grid = new View.GridControl.GridControl();
//Create sudoku data from provided file
Grid model = new Grid(file.Type, file.ID);
model.setValues(file.Data);
//Create new viewmodel for my control and inject it to control
grid.init(new GridVM(model), file.Save);
//Set appearance
grid.Width = 500;
grid.Height = 500;
grid.showLabel(true);
//Create viewbox used to stretch control to desired size and wrap it around my usercontrol
System.Windows.Controls.Viewbox box = new System.Windows.Controls.Viewbox
{Stretch = System.Windows.Media.Stretch.Uniform, Child = grid};
System.Windows.Controls.Grid.SetRow(box, row);
System.Windows.Controls.Grid.SetColumn(box, column);
box.Margin = new Thickness(5);
return box;
}
答案 0 :(得分:1)
在WPF中打印仍然让我觉得有点黑魔法。我已经接触过一些陷阱。一种是如果控件没有设置在屏幕的可见部分并且允许渲染,则控件往往不能正确打印。如果控件没有渲染到屏幕上,那么布局转换等内容将会失败。另一件有用的事情是在您尝试打印的项目上调用“测量和排列”。
grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
grid.Arrange(new Rect(0, 0, grid.Height, grid.Width));
答案 1 :(得分:0)
ViewBox需要计算初始布局。呼叫
box.UpdateLayout();
从generateGridControl
返回之前。请注意,UpdateLayout通常不会强制进行布局更新,但只会影响IsMeasureValid
或IsArrangeValid
为false的元素。无论如何,这里它将起作用,因为它是初始布局。