我有一个这样的页面:
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
here are contents
they are forever absolutely in the center of the screen
no matter of the resolution/size
</StackPanel>
</Grid>
一切都很正常。但是现在我想在左上角添加一个后退按钮。
我不想像这样将网格分成2列和2行:
内容不再绝对居中,甚至我们可以按百分比拆分网格,因为内容有时非常宽,有时非常窄。
我想知道如何保持内容的水平/垂直对齐“居中”,然后设置后退按钮相对于内容的位置。
发布于 2012-08-17 19:35:51
略显老练的ansewer
你也许可以通过将堆叠面板放置在中间,然后设置按钮宽度的左边距为负,以按所需的量移动左边的所有内容来实现。
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-45,0,0,0">
<button with width and padding totalling 45px />
here are contents
they are forever absolutely in the center of the screen
no matter of the resolution/size
</StackPanel>
</Grid>
发布于 2012-08-29 11:08:16
我建议使用包含3列的网格布局,以确保内容居中,并将列宽度设置为*、Auto、*。这将确保主内容始终居中,而不关心按钮的大小。然后,您可以根据需要使用边距来设置分隔。这是我在SL,WPF和Metro中使用的技术。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Right" VerticalAlignment="Top" Content="Do Something"/>
<ContentControl VerticalAlignment="Top" Content="My custom content"/>
</Grid>
https://stackoverflow.com/questions/12004838
复制相似问题