2009-05-28 323 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>