locked
How stretch textbox to 100% of the column RRS feed

  • Question

  •  I have a textbox in a grid column. How do I stretch my control take up 100% of the column width? Horizontalalignment=stretch doesn't seem to work

    Sunday, March 14, 2010 4:16 AM

Answers

  • How do I stretch my control take up 100% of the column width

    If you don't specify height\width of the element in the Grid row\column, it takes up 100% of space by default. e.g.

    <Grid x:Name="LayoutRoot" Background="White" Width="400" Height="300">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBox Grid.Column="0" Background="Red"/>
      <TextBox Grid.Column="1" Background="Blue"/>
    </Grid>

    This shows two Textboxes each taking up all the space of grid-columns. Please post your xaml to find out problem due to which you are not achieving the desired result.

    Sunday, March 14, 2010 5:03 AM
  •  What are you trying to align? If you set nothing on the textbox, as noted above, the textbox will take up the entire cell making alignment pointless.  Theres nothing to align since it takes up the entire space.  Are you trying to align the text inside the textbox?

    Monday, March 15, 2010 8:19 AM

All replies

  • Hi,

    Make sure that Width and Height properties of TextBox are set to “Auto”. Or use Expression Blend designer and turn on “snapping to snap lines” option and then drag or stretch your TextBox in appropriate Grid Column.

    <TextBox TextWrapping="Wrap" Text="TextBox"/>

    Note:  HorizontalAlignment  is by default "strecth" here.

    Muaz

    Sunday, March 14, 2010 4:47 AM
  • How do I stretch my control take up 100% of the column width

    If you don't specify height\width of the element in the Grid row\column, it takes up 100% of space by default. e.g.

    <Grid x:Name="LayoutRoot" Background="White" Width="400" Height="300">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBox Grid.Column="0" Background="Red"/>
      <TextBox Grid.Column="1" Background="Blue"/>
    </Grid>

    This shows two Textboxes each taking up all the space of grid-columns. Please post your xaml to find out problem due to which you are not achieving the desired result.

    Sunday, March 14, 2010 5:03 AM
  •  It turns out the reason it didn't work was because I set the parent container to left align.

     How do I set the alignment to left align and stretch the control?

    Sunday, March 14, 2010 4:12 PM
  •  What are you trying to align? If you set nothing on the textbox, as noted above, the textbox will take up the entire cell making alignment pointless.  Theres nothing to align since it takes up the entire space.  Are you trying to align the text inside the textbox?

    Monday, March 15, 2010 8:19 AM
  • Hi sffrog25,

    As leifre said, there's nothing to align if you're going to stretch the TextBox, it will take up the whole column space. If you want to align the text in TextBox, you can use TextAlignment property of TextBox.

    Tuesday, March 16, 2010 10:44 PM