locked
Centering text vertically in a textbox RRS feed

  • Question

  • Very simple question: how do I center the text the user enters into a textbox, vertically?  For example, in my app when the user types "g", the lower part of the 'g' disappears.
    Rod
    Friday, April 30, 2010 9:07 PM

All replies

  • Hi Rod,

    I don't know if I understand you right. But can't you set the height to auto and the TextAlignment to Center to solve your problem?

    <TextBox HorizontalAlignment="Left" 
        VerticalAlignment="Bottom" 
        Width="177" Grid.Row="1" 
        Text="g" 
        TextWrapping="Wrap" 
        RenderTransformOrigin="0.5,0.5" 
        TextAlignment="Center">
     <TextBox.RenderTransform>
     <TransformGroup>
      <ScaleTransform/>
      <SkewTransform/>
      <RotateTransform Angle="270"/>
      <TranslateTransform/>
     </TransformGroup>
     </TextBox.RenderTransform>
    </TextBox>
    

    Do you have a short code snippet? Which font type do you use?

    Best regards,

    Martin

    Sunday, May 2, 2010 6:04 PM
  • Here's the code snippet for the textbox:

    <TextBox Margin="24,6,40,7" Grid.Column="1" Grid.Row="1" Text="" TextWrapping="Wrap" Name="txtLastName" MaxLength="25" />

    As to what font I'm using?  Bets me, sorry I don't know.  Can't find it specified anywhere in the XAML.

     


    Rod
    Monday, May 3, 2010 4:50 PM
  • I may have found an answer.  What I found was the row definition only allowed for a fixed height, so I put in the following:

    <RowDefinition Height="0.324*" />

    It looks like that took care of it.  Am I correct that this is what I should have done?

     


    Rod
    Monday, May 3, 2010 8:07 PM
  • Hi Rod,

    I guess you should make sure that your grid row height has always at least the height of you textbox.

    To achieve this you could set the MinHeight value of the Row to the height value of your TextBox. This helps if you implement a fixed layout.

    The following code uses an element property binding on the MinHeight value of Row 0 and provides a possible solution for a more dynamic layout that also avoids conflicts with the margin values of the textbox within row 0:

    <Grid HorizontalAlignment="Right" 
    		Height="92" 
    		Margin="0,115,36,0" 
    		VerticalAlignment="Top" 
    		Width="202">
    	<Grid.RowDefinitions>
    	 <RowDefinition MinHeight="{Binding ActualHeight, ElementName=textBox}" Height="Auto"/>
     	 <RowDefinition/>
    	 <RowDefinition/>
    	 <RowDefinition/>
    	</Grid.RowDefinitions>
    	<TextBox x:Name="textBox" 
    		HorizontalAlignment="Left" 
    		TextWrapping="Wrap" 
    		Text="TextBoggs" 
    		d:LayoutOverrides="Height" 
    		FontFamily="Verdana" 
    		FontSize="18.667" 
    		Margin="0,0,0,6"/>
    </Grid>
    

    Play with the margins and with the fontsize value of the textbox and tell me if it solves your problem. Please note that the Height of Row 0 is set to "Auto" !

    Hope this helps.

    Best regards,

    Martin

    Tuesday, May 4, 2010 1:26 PM