2012-08-17 66 views
0

我有一个网页是这样的:如何在Windows 8应用中根据另一个设置控件的位置?

<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> 

一切工作正常。但是现在我想在左上角添加一个后退按钮。 example

而且我不想电网分成2列和2行是这样的: enter image description here

的内容不再是绝对的中心,甚至我们可以通过拆分%的网格,因为内容有时很宽,有时很窄。

我想知道如何保持内容水平/垂直对齐“居中”,然后将后退按钮的位置设置为相对于内容。

回答

0

稍微哈克ansewer

您可能能够通过在中心定位你的StackPanel来实现,然后设置一个负左边的按钮来换挡一切由所需量留下的宽度...

<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> 
+0

哦!负利润率!我在css中使用了这个技巧,我不知道它在xaml中也有效。我会在下周尝试你的解决方案,非常感谢你。 – 2012-08-17 11:38:27

1

我建议使用带有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> 
+0

+1谢谢,这也适用。 – 2012-08-29 03:20:52

相关问题