2009-05-28 8 views
4

J'ai un formulaire WPF dans lequel j'essaie de créer un formulaire de saisie simple. Deux étiquettes, deux zones de texte et un bouton "Envoyer". J'ai une disposition assez bonne, la seule chose que je ne peux pas obtenir est que mes "étiquettes" soient alignées à droite dans leurs cellules. J'ai essayé à la fois TextAlign = "Right" et HorizontialAlign = "Right", qui déplace le texte à fond, en superposant ma zone de texte, pas simplement en bougeant à l'intérieur de la cellule. Voici le code XAML de la fenêtre.Éléments de grille WPF et texte aligné à droite

<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

Pour vos balises de grille ajouter: ShowGridLines = True cela devrait vous aider à visualiser les limites de vos lignes et colonnes – vidalsasoon

Répondre

11

Votre grille n'a qu'une seule colonne comme écrite. Il aura besoin de deux pour prendre en charge votre paramètre Grid.Column = 1 pour les zones de texte. Ainsi, vous devez ajouter un bloc <ColumnDefinitions>. Avec le XAML tel qu'il est maintenant, WPF jette juste les deux contrôles dans la même colonne, d'où le comportement que vous voyez.

+0

Merci Peter! Intellisense m'a troublé en ne montrant pas les "définitions de colonnes". Je pensais que c'était ce qui était nécessaire, mais je pensais qu'il me manquait quelque chose! Tout est bon! –

2

Voici ce que j'ai trouvé. J'apprends juste WPF moi-même. Comme PeterAllenWebb mentionné, votre problème principal est vous manque le ColumnDefinitions. J'ai également ajouté les attributs TextAlignment="Right" aux deux 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> 
Questions connexes