2013-07-04 26 views
4

我想在Windows Phone 8中水平對齊的堆疊面板中安排兩個組件,一個向左,另一個向右。如何在Windows Phone 8的堆疊面板中將一個組件左對齊和右對齊

我需要左對齊的組件應該在頁面的左側,右對齊的組件應該在頁面的右側。這兩個組件之間應該有差距。

左對齊的組件是靜態的,右對齊的組件是動態的。 靜態組件我把寬度=自動和動態組件的剩餘空間。

以下是我的代碼。

<Border x:Name="DataBorder" Grid.Row="1" BorderBrush="Gray" CornerRadius="5,5,5,5" BorderThickness="2,2,2,2" Margin="10,30,10,10"> 
     <StackPanel x:Name="settingsPanel" Orientation="Vertical"> 
      <StackPanel x:Name="langPanel" Orientation="Horizontal"> 
       <TextBlock x:Name="langTextBlock" Text="{Binding Path=LocalizedResources.DefaultLanguageText, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Foreground="#FF501F6E" FontWeight="Bold" FontSize="25" Width="Auto"/> 

       <Button Content="English" Height="70" HorizontalAlignment="Right" Margin="5,1,0,0" Name="langbutton" VerticalAlignment="Center" MinWidth="160" Foreground="#FF501F6E" Click="language_Btn_clicked" BorderBrush="{x:Null}"> 
        <Button.Background> 
         <ImageBrush Stretch="Fill" ImageSource="Images/blank_button_nav.png" /> 
        </Button.Background> 
       </Button> 

       <!--<Image x:Name="arrow" Stretch="Fill" Margin="0,0,0,0" Source="Images/arrow.png" Height="20"/>--> 
      </StackPanel> 

      <StackPanel x:Name="pagePanel" Orientation="Horizontal"> 
       <TextBlock x:Name="pageTextBlock" Text="{Binding Path=LocalizedResources.PerPageText, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" Foreground="#FF501F6E" FontWeight="Bold" FontSize="25" Width="Auto"/> 

       <Button Content="25" Height="70" HorizontalAlignment="Right" Margin="5,1,0,0" Name="pagebutton" VerticalAlignment="Center" MinWidth="160" Foreground="#FF501F6E" Click="page_Btn_clicked" BorderBrush="{x:Null}"> 
        <Button.Background> 
         <ImageBrush Stretch="Fill" ImageSource="Images/blank_button_nav.png" /> 
        </Button.Background> 
       </Button> 
      </StackPanel> 
</StackPanel> 
</Border> 

但右對齊的組件即將到達左對齊的組件。

回答

19

這是StackPanel如何設計佈局內容。如果你想將一個向左和向右對齊,我會建議使用一個網格控件。

您可以通過以下兩種方式之一

<Grid > 
    <Button Content="One" Width="75" HorizontalAlignment="Left"/> 
    <Button Content="Two" Width="75" HorizontalAlignment="Right"/> 
</Grid> 

或選項2

<Grid > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <Button Content="One" /> 
    <Button Content="Two" Grid.Column="1"/> 
</Grid> 

選擇一個具有按鍵相互重疊的可能性佈局網格。選項二的優點是每個按鈕都具有相同的寬度。

0

我想與您的代碼的主要問題是堆疊面板只需要它需要適應成分寬度......有很多可以方式來解決這個..

1)製作的水平對齊方式stackpanel到strech。

2)通過在其中任何一個邊框中設置邊距來設置文本塊和按鈕之間的邊距。

3)你可以使用網格列而不是堆疊面板來解決問題。

相關問題