Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a wpf text box to resize automatically when the user changes the size of dialog box?

Tags:

c#

wpf

How do I set a wpf text box to resize automatically when the user changes the size of dialog box?

 <Window x:Class="MemoPad.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="LightGray" 
    Title="Window1" Height="350" Width="700" >
<StackPanel Orientation="Vertical">
    <Menu DockPanel.Dock ="Right">
        <MenuItem Header="Find" x:Name="gMNuFind" />
    </Menu>
    <Button Content=" Find " 
          Margin="5,10,5,5"
          x:Name="gBuFind" 
          />
    <TextBox Margin="0,0,0,0"
          HorizontalAlignment="Left"
          VerticalAlignment="Top" 
          MinHeight="270" MinWidth="690"                  
          x:Name = "gTBxInfo" 
          TextWrapping="Wrap"
          AcceptsReturn="True"
          ScrollViewer.VerticalScrollBarVisibility="Auto" 
          />
</StackPanel>

like image 989
ttom Avatar asked Jan 20 '15 21:01

ttom


2 Answers

Or change StackPanel to Grid

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="LightGray" 
    Title="Window1" Height="350" Width="700" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="Find" x:Name="gMNuFind" />
        </Menu>
        <Button Grid.Row="1" Content=" Find " 
          Margin="5,10,5,5"
          x:Name="gBuFind" 
          />
        <TextBox Grid.Row="2" Margin="0,0,0,0"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch" 
          MinHeight="270" MinWidth="690"                  
          x:Name = "gTBxInfo" 
          TextWrapping="Wrap"
          AcceptsReturn="True"
          ScrollViewer.VerticalScrollBarVisibility="Auto" 
          />
    </Grid>
</Window>
like image 181
rraszewski Avatar answered Oct 26 '22 22:10

rraszewski


Remove MinHeight and MinWidth from the TextBox, and change HorizonalAlignment to Stretch

<TextBox Margin="0,0,0,0"
  HorizontalAlignment="Stretch"
  VerticalAlignment="Top"               
  x:Name = "gTBxInfo" 
  TextWrapping="Wrap"
  AcceptsReturn="True"
  ScrollViewer.VerticalScrollBarVisibility="Auto" />

Edit:

If you want to resize the TextBox in both directions (horizontally and vertically), you would have to use a different container other than StackPanel, so that the TextBox sizing is independent.

Something like this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Menu>
        <MenuItem Header="Find" x:Name="gMNuFind" Grid.Row="0"/>
    </Menu>
    <Button x:Name="gBuFind" 
            Content=" Find " 
            Margin="5,10,5,5"     
            Grid.Row="1"/>
    <TextBox x:Name = "gTBxInfo" 
             Margin="0,0,0,0"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"               
             TextWrapping="Wrap"
             AcceptsReturn="True"
             ScrollViewer.VerticalScrollBarVisibility="Auto" 
             Grid.Row="2"/>
</Grid>
like image 35
d.moncada Avatar answered Oct 26 '22 23:10

d.moncada