2013-10-09 78 views
0

我想要在彼此之上佈置2個標籤,然後在水平方向上彼此相鄰但在標籤下方佈置兩個按鈕(以下是我想要的樣例示例等)如何在使用WPF時嵌套在groupbox中的控件

(所有項目都居中)

Welcome 
Please Select A Language 

English  Spanish 

這是我的XAML

<Window x:Class="Probation_Sign_In.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="768" Width="1024" Closing="Window_Closing" ResizeMode="NoResize" Topmost="True" WindowStartupLocation="CenterScreen" WindowState="Maximized"> 
    <Grid>   
     <GroupBox Header="GroupBox" HorizontalAlignment="Left" Height="492" Margin="10,10,0,0" VerticalAlignment="Top" Width="505"> 
      <StackPanel> 
       <Label x:Name="labelWelcome" Content="Welcome." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Margin="136,55,0,0"/> 
       <Label x:Name="labelWelcomeMessage" Content="Please select a language." HorizontalAlignment="Left" Margin="50,193,0,0" VerticalAlignment="Top" FontSize="36"/> 
       <Button x:Name="englishButton" Content="English" HorizontalAlignment="Center" Margin="109,410,767,297" VerticalAlignment="Center" Width="142" FontSize="20" TouchUp="englishButton_TouchUp" Click="englishButton_Click"> 
        <Button.Background> 
         <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
          <GradientStop Color="#FFF3F3F3" Offset="0"/> 
          <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
          <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
          <GradientStop Color="#FF71B5BB" Offset="1"/> 
         </LinearGradientBrush> 
        </Button.Background> 
       </Button> 
       <Button Content="Español" HorizontalAlignment="Center" Margin="269,410,607,300" VerticalAlignment="Center" Width="142" FontSize="20" Height="30"> 
        <Button.Background> 
         <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
          <GradientStop Color="#FFF3F3F3" Offset="0"/> 
          <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
          <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
          <GradientStop Color="#FF71B5BB" Offset="1"/> 
         </LinearGradientBrush> 
        </Button.Background> 
       </Button> 
      </StackPanel> 
     </GroupBox> 


    </Grid> 
</Window> 

以上XAML不給我想要的結果。但是,所有的控制必須是分組框中

回答

3

內環繞你Button控制在另一個StackPanel及其Orientation屬性設置爲Horizontal

<Grid> 
    <GroupBox Header="GroupBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="505"> 
     <StackPanel> 
      <Label x:Name="labelWelcome" Content="Welcome." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Margin="136,10,0,0"/> 
      <Label x:Name="labelWelcomeMessage" Content="Please select a language." HorizontalAlignment="Left" Margin="50,0,0,0" VerticalAlignment="Top" FontSize="36"/> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
       <Button x:Name="englishButton" Content="English" Width="142" FontSize="20" Margin="5" TouchUp="englishButton_TouchUp" Click="englishButton_Click"> 
        <Button.Background> 
         <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
          <GradientStop Color="#FFF3F3F3" Offset="0"/> 
          <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
          <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
          <GradientStop Color="#FF71B5BB" Offset="1"/> 
         </LinearGradientBrush> 
        </Button.Background> 
       </Button> 
       <Button Content="Español" Width="142" FontSize="20" Height="30" Margin="5"> 
        <Button.Background> 
         <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
          <GradientStop Color="#FFF3F3F3" Offset="0"/> 
          <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
          <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
          <GradientStop Color="#FF71B5BB" Offset="1"/> 
         </LinearGradientBrush> 
        </Button.Background> 
       </Button> 
      </StackPanel> 
     </StackPanel> 
    </GroupBox> 
</Grid> 

編輯: 改變的邊距值,以便控制堆棧密切。玩Margin屬性來獲得你想要的空間。

+0

優秀的,但是當我拖動 「選擇語言」 標籤時,它彈回來了。 –

+0

您的'Label'控件都應用了頂部'Margin'值。我已經改變了我的答案,以便控制器靠得更近。 – 2013-10-09 20:08:17

0

試試這個

   </Grid.ColumnDefinitions> 
      <Button x:Name="englishButton" Content="English" HorizontalAlignment="Center" VerticalAlignment="Center" Width="142" FontSize="20" Click="englishButton_Click"> 
       <Button.Background> 
        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
         <GradientStop Color="#FFF3F3F3" Offset="0"/> 
         <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
         <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
         <GradientStop Color="#FF71B5BB" Offset="1"/> 
        </LinearGradientBrush> 
       </Button.Background> 
      </Button> 
      <Button Grid.Column="1" Content="Español" HorizontalAlignment="Center" VerticalAlignment="Center" Width="142" FontSize="20" Height="30"> 
       <Button.Background> 
        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
         <GradientStop Color="#FFF3F3F3" Offset="0"/> 
         <GradientStop Color="#FFEBEBEB" Offset="0.5"/> 
         <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
         <GradientStop Color="#FF71B5BB" Offset="1"/> 
        </LinearGradientBrush> 
       </Button.Background> 
      </Button> 
      </Grid> 
     </StackPanel> 
    </GroupBox> 


</Grid>