我有一个网格,我希望行高是自动的。它总是工作正常,但现在我在每行的顶部和底部都有很多空间。我需要它们几乎与文本的大小完全相同。我正在从文件中读取数据并在循环中创建行。
我的代码如下。根据这个网站的建议,我尝试使用Trim(),但没有做任何事情。我也尝试使用GridLength(15,GridUnitType.Pixel)手动设置大小(虽然我更宁愿不这样做),虽然行高度较小,但我可以从剪切文本中看到仍然有这个白色边框。我也读过有关BorderThickness的内容,但这似乎是标签而不是网格。任何见解将不胜感激。
string[] allFile = File.ReadAllLines(textFile);
//number of lines
int count = Convert.ToInt32(allFile[0]);
for (int i = 0; i < count; i++)
{
RowDefinition rowNew1 = new RowDefinition();
rowNew1.Height = new GridLength(1, GridUnitType.Auto);
myGrid.RowDefinitions.Add(rowNew1);
Label lbl = new Label();
lbl.Content = allFile[i + 1];
lbl.Margin = new Thickness(10, 0, 0, 0);
myGrid.Children.Add(lbl);
Grid.SetRow(lbl, i + 5); //have a number of rectangles placed in xaml
}
这是xaml代码,如果它有任何区别(虽然我怀疑它)
<Grid Name="myGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="350"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
<RowDefinition Height="1"/>
<RowDefinition Height="20"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<TextBlock Name="pnp" VerticalAlignment="Top" Height="20" Grid.Row="0" Grid.ColumnSpan="9" Background="MistyRose" Margin="0,0,0,0" FontSize="12" FontWeight="Bold"></TextBlock>
<Rectangle Grid.Row="3" Grid.ColumnSpan="9" Fill="AliceBlue"></Rectangle>
<TextBlock Name="PatientSnapshot" Height="20" Foreground="Black" FontSize="14" Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="3" FontWeight="Bold" Margin="10,0,0,0">Text</TextBlock>
<Rectangle Grid.ColumnSpan="9" Grid.Row="1" Grid.RowSpan="2" VerticalAlignment="Top" Height="47" Fill="LightGray"></Rectangle>
<Rectangle Grid.ColumnSpan="9" Grid.Column="0" Grid.Row="4" Height="20" Fill="WhiteSmoke"></Rectangle>
<TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" FontSize="10" Margin="10,0,0,0" VerticalAlignment="Center">Text</TextBlock>
<TextBlock Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" FontSize="10" Margin="0,0,0,0" VerticalAlignment="Center">Text</TextBlock>
<TextBlock Grid.Row="4" Grid.Column="5" Grid.ColumnSpan="4" FontSize="10" Margin="0,0,0,0" VerticalAlignment="Center">Text</TextBlock>
</Grid>