我编写了以下代码来创建固定文档。
在页面上,我添加了一个堆栈面板。堆栈面板包含一些文本块和网格。网格应该有三列:10%、45% 和 45%。
然而,网格离开了页面,但我不知道为什么。有什么建议么?
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);
我尝试仅将网格添加到堆栈面板,仅将网格本身添加到没有堆栈面板,并且我还尝试将这些值用于 GridLenth,但没有任何区别:
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);