WPF - How to stop a TextBox from autosaving?

I have a text box in my visual tree as follows.

  • Window
    • Grid
      • ListBox
        • ItemTemplate
          • DataTemplate
            • Grid
              • Grid
                • Textbox ...
The text field 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 associated variable (LyricsForDisplay), all the elements in the list expand the width of their text fields / grids so that you can see the entire line if you use the scroll bar at the bottom that appears ...

What I would like to do is to make sure that the fields / grids change only if the user stretches the window. NOT when long text is entered (it may just wrap around.)

Does anyone know how to get functionality?

+7
source share
5 answers

The following works:

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

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

+9
source

Something should limit the horizontal width available for text fields, in which case you want to stop the ListBox from growing horizontally unlimitedly:

 <ListBox HorizontalScrollBarVisibility="Disabled" 
+4
source

Unfortunately, a regular TextBox does not allow auto-cut to match the parent, but DOES NOT autoresist when the text does not fit.

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

In the .xaml.cs file:

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

Then in the .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> 
+2
source

two change the code:
1- Add a border tag with your grid.column and grid.row size, which you are sideways.
2- the width and height of the text field set for 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> 
+1
source

Try setting the MaxWidth property in the text box

 <TextBox Height="Auto" Text="{Binding Path=LyricsForDisplay}" MinHeight="50" MaxHeight="400" Visibility="Visible" VerticalScrollBarVisibility="Auto" MaxWidth="100" IsReadOnly="True" AllowDrop="False" TextWrapping="WrapWithOverflow"> </TextBox> 
0
source

All Articles