WPF - How to stop TextBox from autosizing?

自作多情 提交于 2019-11-29 09:56:33

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

Kent Boogaart

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"

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>

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!