0

I have written the following code to create a fixed document.

On the page I add a stack panel. The stack panel contains some textblocks and a grid. The grid is supposed to have three columns: 10%, 45% and 45%.

However the Grid goes off the page but I don't know why. Any suggestions?

FixedDocument fd = new FixedDocument();

Grid grd = new Grid();
grd.ShowGridLines = true;
grd.HorizontalAlignment = HorizontalAlignment.Stretch;

RowDefinition row_def;
ColumnDefinition col_def;

col_def = new ColumnDefinition();
col_def.Width = new GridLength(0.1, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);
col_def = new ColumnDefinition();
col_def.Width = new GridLength(0.45, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);
col_def = new ColumnDefinition();
col_def.Width = new GridLength(0.45, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);
...
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(32, 20, 20, 20);
sp.Children.Add(grd);
...
Size pgSize = new Size(96 * 8.27, 96 * 11.69);
FixedPage fp = new FixedPage();
fp.Width = pgSize.Width;
fp.Height = pgSize.Height;
fp.Children.Add(sp);

PageContent pc = new PageContent();
pc.Child = fp;
fd.Pages.Add(pc);

I tried adding just the grid to the stack panel, just the grid by itself with no stack panel, and I also tried using these values for GridLenth but it made no difference:

col_def = new ColumnDefinition();
col_def.Width = new GridLength(10, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);
col_def = new ColumnDefinition();
col_def.Width = new GridLength(45, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);
col_def = new ColumnDefinition();
col_def.Width = new GridLength(45, GridUnitType.Star);
grd.ColumnDefinitions.Add(col_def);

Fixed Documemt View

Ross Kelly
  • 477
  • 1
  • 6
  • 23
  • I guess the problem is using the `StackPanel` as it let its content push its own margins. Try replacing that `StackPanel` with `Grid` with number or rows and cols accordingly to your needs – trix Jun 25 '20 at 13:04
  • I tried adding nothing but the grid to the page but its the same. – Ross Kelly Jun 25 '20 at 13:12
  • Since you know the page width, you can simply set the width of the StackPanel: `sp.Width = pgSize.Width - 52;` – Clemens Jun 25 '20 at 13:25
  • @Clemens that does the trick. thanks for the suggestion. strange. i still don't know why the grid or stackpanel goes over the page width. – Ross Kelly Jun 25 '20 at 14:35

0 Answers0