14

I have a textbox in my visual tree as follows..

  • Window
    • Grid
      • ListBox
        • ItemTemplate
          • DataTemplate
            • Grid
              • Grid
                • Textbox...
The textbox is defined as..
<TextBox Height="Auto" 
         Text="{Binding Path=LyricsForDisplay}" 
         MinHeight="50" 
         MaxHeight="400"  
         Visibility="Visible" 
         VerticalScrollBarVisibility="Auto" 
         IsReadOnly="True" 
         AllowDrop="False" 
         TextWrapping="WrapWithOverflow">
</TextBox>

When long text is added to the bound variable (LyricsForDisplay) all of the items in the listbox expand their textboxes/grids width's to allow for the entire string to be seen if you use the scrollbar on bottom that appears...

What I would like to do is make it so the boxes/grids only resize if the user stretches the window .. NOT when a long text is entered (it could just wrap around..)

Does anyone know how to obtain the functionality?

decyclone
  • 30,394
  • 6
  • 63
  • 80
Ryan
  • 241
  • 1
  • 5
  • 14

5 Answers5

10

Unfortunately, the regular TextBox doesn't allow autoresize to fit the parent but NOT autoresize when the text doesn't fit.

To solve this problem, you can use a custom TextBox that reports a desired (0, 0) size. It's an ugly hack, but it works.

In your .xaml.cs file:

public class TextBoxThatDoesntResizeWithText : TextBox
{
    protected override Size MeasureOverride(Size constraint)
    {
        return new Size(0, 0);
    }
}

Then, in your .xaml file:

<Window x:Class="YourNamespace.YourWindow"
    ...
    xmlns:local="clr-namespace:YourNamespace">
        ...
        <local:TextBoxThatDoesntResizeWithText Height="Auto" 
                                               Text="{Binding Path=LyricsForDisplay}" 
                                               MinHeight="50" 
                                               MaxHeight="400"  
                                               Visibility="Visible" 
                                               VerticalScrollBarVisibility="Auto" 
                                               IsReadOnly="True" 
                                               AllowDrop="False" 
                                               TextWrapping="WrapWithOverflow">
        </local:TextBoxThatDoesntResizeWithText>
        ...
</Window>
ajarov
  • 149
  • 2
  • 5
  • This was the only thing that worked for me! This feels like behaviour which should be part of the standard control. Cheers, @ajarov, saved me a lot of time spinning my wheels! – Pseudonymous Nov 02 '18 at 09:37
  • Helped me as well! Easy solution, even though a bit 'hacky', thanks! – mephisto123 Jan 14 '20 at 12:51
  • 1
    Solved my problem too, after a lot of hair-tearing. In recent versions of C# the function can be a one-liner: `protected override Size MeasureOverride( Size constraint ) => Size.Empty;` – sidbushes Jun 16 '21 at 14:53
10

The following works:

<ListBox Name="ListBox1"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid>
                    <TextBox TextWrapping="Wrap"></TextBox>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Notice the use of ScrollViewer.HorizontalScrollBarVisibility="Disabled" and TextWrapping="Wrap".

decyclone
  • 30,394
  • 6
  • 63
  • 80
4

Something needs to contrain the horizontal width available to the TextBoxes, in this case you want to stop the ListBox from growing horizontally indefinitely:

<ListBox HorizontalScrollBarVisibility="Disabled"
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
2

two change the code:
1- add border tag with your grid.column and grid.row size that you are neads.
2- width and height of textbox set to it.
sample:

 <Border x:Name="b" Margin="5"/>


  <TextBox Height="Auto" 
             Text="{Binding Path=LyricsForDisplay}" 
             MinHeight="50" 
             Width="{Binding ActualWidth, ElementName=b}" 
             Height="{Binding ActualHeight, ElementName=b}" 
             Visibility="Visible" 
             VerticalScrollBarVisibility="Auto" 
             IsReadOnly="True" 
             AllowDrop="False" 
             TextWrapping="Wrap">
    </TextBox>
Ali Rasouli
  • 1,705
  • 18
  • 25
0

Try setting the the MaxWidth Property in Your Textbox

<TextBox Height="Auto" Text="{Binding Path=LyricsForDisplay}" MinHeight="50" MaxHeight="400"  Visibility="Visible" VerticalScrollBarVisibility="Auto" MaxWidth="100" IsReadOnly="True" AllowDrop="False" TextWrapping="WrapWithOverflow">                            </TextBox>
biju
  • 17,554
  • 10
  • 59
  • 95