WPF - How to stop TextBox from autosizing?

Ryan picture Ryan · Dec 16, 2010 · Viewed 13.2k times · Source

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?

Answer

decyclone picture decyclone · Dec 17, 2010

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".