如何在WPF中文本太长时水平自动展开文本框?

时间:2022-04-08 12:34:53

How do I configure the textbox so that it will automatically expand itself when the inputted text is too long (longer than the width of the textbox)?

如何配置文本框,以便在输入的文本太长(长于文本框的宽度)时自动展开?

I don't want to wrap the text to a new line under, but instead, I want to expand the textbox automatically.

我不想将文本换行到新行下,而是想要自动扩展文本框。

2 个解决方案

#1


1  

Don't give any fixed width to your textbox, it will resize itself per text string length. Just give columnWidth of your container to be Auto where its placed.

不要给文本框赋予任何固定宽度,它会根据文本字符串长度调整自身大小。只需将容器的columnWidth设置为自动放置的位置即可。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="I don't care about width anymore..!!!"/>
</Grid>

This will work as text suggested.

这将按照文本建议的方式工作。

#2


-1  

Very simple solution use built-in wordwrap function.

非常简单的解决方案使用内置的wordwrap功能。

#1


1  

Don't give any fixed width to your textbox, it will resize itself per text string length. Just give columnWidth of your container to be Auto where its placed.

不要给文本框赋予任何固定宽度,它会根据文本字符串长度调整自身大小。只需将容器的columnWidth设置为自动放置的位置即可。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="I don't care about width anymore..!!!"/>
</Grid>

This will work as text suggested.

这将按照文本建议的方式工作。

#2


-1  

Very simple solution use built-in wordwrap function.

非常简单的解决方案使用内置的wordwrap功能。