Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make textbox resize as the window resize

Tags:

wpf

Any property to set to make textbox to resize according to the window size?

like image 581
user496949 Avatar asked Dec 21 '10 03:12

user496949


2 Answers

Layout in WPF is heavily influenced by the parent container. For example, if you are creating a form with labels and input fields, consider using a Grid panel. Controls in WPF by default resize according to the layout behavior of their parent. Here is an example of a window with two labeled text boxes and two buttons that resize along with the window.

<Window>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="Contact Name" Grid.Row="0" Grid.Column="0" />
        <TextBox Grid.Row="0" Grid.Column="1" />

        <Label Content="Contact Location" Grid.Row="1" Grid.Column="0" />
        <TextBox Grid.Row="1" Grid.Column="1" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1">
            <Button Content="OK" Width="75" Height="24" Margin="3" />
            <Button Content="Cancel" Width="75" Height="24" Margin="3" />
        </StackPanel>

    </Grid>
</Window>

Or if you wanted something similar to the address bar layout of a browser, you could do something like:

<Window>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Button Content="Back" DockPanel.Dock="Left" />
            <Button Content="Forward" DockPanel.Dock="Left" />
            <Button Content="Refresh" DockPanel.Dock="Right" />
            <TextBox /> <!-- fill is assumed for last child -->
        <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" />
        <WebBrowser /> <!-- fill is assumed for last child -->
    </DockPanel>
</Window>

Note that in the above example, I nested two DockPanel's. It could also have been achieved with a Grid but the markup would have been much more cluttered. If you are new to WPF, I'd highly suggest playing around with the various panels available to you. Once you learn when to apply a particular panel to a particular layout, it makes WPF much easier to work with.

like image 140
Josh Avatar answered Sep 21 '22 01:09

Josh


It could be little tricky because of few constraints.

  1. Text box you cant put width auto (in my case if some one copy paste long string it goes out of boundary because of Auto Width).
  2. I can't keep it default because in case of no text it's very small in width, So I needed MinWidth.
  3. At the same time you have to have a static width representation, because we want to wrap the text for particular line.

I tried this solution which makes your width bounded to parent ( I agree there could be better solutions as well, but this was easy and worked fine)

<Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" Name="LeftColumnDefinition" />
      <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Column="0" BorderThickness="3" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" ClipToBounds="True"
                         TextWrapping="Wrap" Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"  MinWidth="560" MinHeight="57" AcceptsTab="True" Foreground="{StaticResource darkBlueBrush}">FIX message... </TextBox>
     <Button Margin="2,0,0,0"  Grid.Column="1" Content="Publish FIX Message" Name="PublishFIX" Click="publishFix_Click" HorizontalAlignment ="Center" Height="25"/>

</Grid>

So only good thing I did was, I binned the

Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"

to <ColumnDefinition Width="*" Name="LeftColumnDefinition" />

like image 27
Rohit Sachan Avatar answered Sep 19 '22 01:09

Rohit Sachan