我正在处理其内容是网格的WPF控件。我对WPF比较陌生,所以我想知道下面是否是正确的方法。WPF网格布局和单元格内容边距
我放置两个标签中的网格,都在同一列中,但相邻行:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="UntitledProject8.Window1"
x:Name="Window"
Title="Window1"
Width="200" Height="200">
<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100"/>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="1.23" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
<Label Grid.Row="1" Content="45" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Top"/>
</Grid>
我设置标签垂直对准,使得在零行标签被对准到底部,第1排的标签与顶部对齐。
现在,这与我想要的很接近,但我需要第1行中标签的实际文本更接近第零行中的标签文本。要做到这一点,我在第1行标签的边距设置为负值:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="UntitledProject8.Window1"
x:Name="Window"
Title="Window1"
Width="200" Height="200">
<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100"/>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="1.23" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
<Label Grid.Row="1" Content="45" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,-20,0,0"/>
</Grid>
那是正确的方式做到这一点?当然,我上面的例子是简单的,但随着网格内容的增长和变得更加复杂(例如包括其他布局容器),为控制边界设置不同的值是使相邻单元格更接近或更远的正确方法?
我只是有点担心,因为我尽我所能避免硬编码任何类型的“设计器”值,就像我在使用WinForms时(例如为位置和尺寸值设置确切的坐标系)一样,并且希望让布局经理来照顾它。但是,看起来设定保证金是唯一的出路。
感谢您的帮助:)