2009-05-28 90 views
4

我有一個WPF表單,我試圖做一個簡單的輸入表單。兩個標籤,兩個文本框和一個「提交」按鈕。我的佈局非常好,唯一不能得到的是我的「標籤」在它們的單元格內右對齊。我已經嘗試了TextAlign =「Right」和Horizo​​ntialAlign =「Right」,它將文本一路移動,覆蓋文本框,而不僅僅是在單元格內移動。以下是該窗口的XAML。WPF Grid Items和右對齊文本

<Window x:Class="MyWebKeepAliveDesktop.Login" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MyWebKeepAlive Desktop - Login" WindowStyle="None" AllowsTransparency="true" Height="200" Width="400" > 

    <Border Background="#50FFFFFF" CornerRadius="7" BorderBrush="{StaticResource WindowFrameBrush}" BorderThickness="2,0,2,2"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30" /> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Border Background="{StaticResource WindowFrameBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
       CornerRadius="5,5,0,0" Margin="-1,0,-1,0" MouseLeftButtonDown="DragWindow"> 
       <Grid> 
        <TextBlock Foreground="White" FontWeight="Bold" VerticalAlignment="Center" Margin="10,2,10,2" 
          Text="MyWebKeepAlive Desktop Login"/> 
        <Button Content="X" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" FontSize="7" 
        Width="15" Height="15" Padding="0" Command="ApplicationCommands.Close"/> 
       </Grid> 
      </Border> 
      <Grid Grid.Row="1" Width="350" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="35" /> 
        <RowDefinition Height="25" /> 
        <RowDefinition Height="25" /> 
        <RowDefinition Height="10" /> 
        <RowDefinition Height="30" /> 
       </Grid.RowDefinitions> 
       <TextBlock TextAlignment="center" Text="Please provide your username/password that is used on the MyWebKeepAlive.com site to login." TextWrapping="Wrap" Grid.Row="0" Grid.ColumnSpan="2" /> 
       <TextBlock Text="Username" FontWeight="Bold" Grid.Row="1" Grid.Column="0"/> 
       <TextBox Name="txtUsername" Width="150" Grid.Row="1" Grid.Column="1" /> 
       <TextBlock Text="Password" FontWeight="Bold" Grid.Row="2" /> 
       <TextBox Name="txtPassword" Width="150" Grid.Row="2" /> 
       <Button Name="btnLogin" Grid.Row="4" Grid.ColumnSpan="2"> 
        <TextBlock Text="Login" /> 
       </Button> 
      </Grid>    
     </Grid> 
    </Border> 
</Window> 
+1

添加到您的網格標籤:ShowGridLines = True這應該可以幫助您查看行和列的限制 – vidalsasoon 2009-05-28 20:14:16

回答

11

您的網格只有一列寫入。它需要兩個才能支持您爲文本框設置Grid.Column = 1。因此,您需要添加<ColumnDefinitions>塊。用現在的方式使用XAML,WPF只是將兩個控件都引入同一列,因此您看到的行爲。

+0

感謝Peter! Intellisense通過不顯示「ColumnDefinitions」而困惑了我。我雖然那是需要的,但是我以爲我錯過了一些東西!一切都很好! – 2009-05-28 20:49:50

2

這是我想出來的。只是自己學習WPF。由於提到PeterAllenWebb,您的主要問題是您缺少ColumnDefinitions。我還將TextAlignment="Right"屬性添加到了兩個TextBlocks

<Grid Grid.Row="1" Width="350" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="35" /> 
       <RowDefinition Height="25" /> 
       <RowDefinition Height="25" /> 
       <RowDefinition Height="10" /> 
       <RowDefinition Height="30"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock TextAlignment="center" Text="Please provide your username/password that is used on the MyWebKeepAlive.com site to login." TextWrapping="Wrap" Grid.Row="0" Grid.ColumnSpan="2" /> 
      <TextBlock Text="Username" TextAlignment="Right" FontWeight="Bold" Grid.Row="1" Grid.Column="0"/> 
      <TextBox Name="txtUsername" Width="150" Grid.Row="1" Grid.Column="1" /> 
      <TextBlock Text="Password" TextAlignment="Right" FontWeight="Bold" Grid.Row="2" /> 
      <TextBox Name="txtPassword" Width="150" Grid.Row="2" Grid.Column="1"/> 
      <Button Name="btnLogin" Grid.Row="4" Grid.ColumnSpan="2"> 
       <TextBlock Text="Login" /> 
      </Button> 
     </Grid>