Reputation: 515
From past 3-4 days i am trying to learn WPF. As a sample xaml ,i tried to create a grid and place some controls in it in the form of textblocks and text boxes.
The problem i am facing is that when i keep on enter the texts in the text boxes ,then my text boxes width keep on increasing,which in turn will ruin my whole form.
Can anyone please help me fixing the issue.
Here is my sample XAML Code which consists of text boxes:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" FontSize="25" Margin="5 30 130 0"/>
<TextBox Grid.Row="2" FontSize="25" Grid.Column="1" Grid.ColumnSpan="2" Margin="5 30 130 0"/>
<TextBox Grid.Row="3" FontSize="25" Grid.Column="1" Margin="5 30 160 0"/>
<TextBox Grid.Row="5" FontSize="25" Grid.Column="1" Grid.ColumnSpan="2" Margin="5 30 130 0"/>
</Grid>
Please anyone help me with this regard.All sorts of help will be appreciated.
Upvotes: 1
Views: 1534
Reputation: 45096
Are you sure the TextBox is getting bigger or is it just filling up with text?
For test put a border brush on it and HorizontalContentAlignment="Stretch".
If you want to constrain the width then set width otherwise it is going to use all available space.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Text="text" BorderBrush="Red" BorderThickness="2" HorizontalContentAlignment="Stretch" />
</Grid>
Upvotes: 1
Reputation: 14002
You can use AcceptsReturn
and TextWrapping
properties on TextBox
to allow newlines and allow text to wrap when the text size exceeds the width of the TextBox
respectively
e.g.
<TextBox TextWrapping="Wrap" AcceptsReturn="true" />
AcceptsReturn
ensures that the user can press return instead of CTRL + Return to put a newline in the text box. Otherwise the textbox loses focus and the default button is pressed as per the standard behaviour. (I think if there is no default button a newline is inserted anyway)
Upvotes: 2