tags:

views:

418

answers:

2

I have a problem with a TextBox in an application...

A window has a Grid with two columns. The left column contains a control with a constant width but with a height that adapts. The right column contains a TextBox that takes up all remaining space in the Grid (and thereby in the Window).

The Grid is given a minimal width and height and is wrapped within a ScrollViewer. If the user resizes the window to be smaller than the minimal widht/height of the Grid, scrollbars are displayed.

This is exactly how I want it to be. However, a problem occurs when the user starts typing text. If the text is to long to fit in one line in the TextBox, I want the text to wrap. Therefore I set TextWrapping="Wrap" on the TextBox. But since the TextBox has an automatic width and is wrapped in a ScrollViewer (its actually the whole Grid that is wrapped), the TextBox just keeps expanding to the right.

I do want the TextBox to expand if the window is expanded, but I don't want the TextBox to expand by the text. Rather the text should wrap inside the available TextBox. If the text don't fit within the TextBox height, a scrollbar should be displayed within the TextBox.

Is there a way to accomplish this?

Below is some code that shows my problem.

<Window x:Class="AdaptingTextBoxes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="300" Width="400" Background="DarkCyan">
<Grid Margin="10" Name="LayoutRoot">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid MinWidth="300" MinHeight="200">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Margin="0,0,10,0" Content="Button" Width="100" />

            <TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
        </Grid>
    </ScrollViewer>
</Grid>
</Window>
+3  A: 

You could use an invisible border (its hacky but it works - its how I tend to sort out dynamic textbox sizes):

<Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
                <TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
Leom Burke
I can't get your code to work. As I enlarge the window, the TextBox follows as it should, but when I try to make the window smaller again the TextBox doesn't get any smaller (instead, the scrollbars of the scrollviewer are shown).
haagel
My apologies - the border needed a margin - I have no w edited my answer. Thats what I get for doing an answer from memory - I have tested this and it does what you require. I am sure you can change the margins and paddings in your real app so that the margin on the border becomes part of the app.
Leom Burke
Thanks a lot Leom Burke! That works fine! :)
haagel
+1  A: 

Have you tried setting the MaxWidth property on just the TextBox?

Edit after OP's comment

I would try getting rid of the ScrollViewer. The sizing used in the Grid's layout should take care of re-sizing and the scroll bar settings on the TextBox should take care of the rest.

Mike Two
I don't want the TextBox to have a max width. I want it to fill upp all the remaining space in the Grid, however big it might be.
haagel
@haagel - so why the Horizontal scroll bar on the outer `ScrollViewer`? That's just encouraging everything to expand horizontally forever? As you've seen the `TextBox` has a scroll bar as well. I would get rid of the `ScrollViewer`.
Mike Two
MaxWidth should be correct, you can try to bind MaxWidth to Grid.Width
marco.ragogna
@Mike Two - My sample code is a simplification of my real app. It's actually a complete form that is wrapped with a ScrollViewer. The reason is that the form must be possible to view even if the application window is small. Thus the user must be able to scroll the form when the window is to small for the form to fit.
haagel