WPF - 如何阻止 TextBox 自动调整大小?
我的视觉树中有一个文本框,如下所示。
窗口
网格
列表框
项目模板
数据模板
网格
网格
文本框
...
<TextBox Height="Auto"
Text="{Binding Path=LyricsForDisplay}"
MinHeight="50"
MaxHeight="400"
Visibility="Visible"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
AllowDrop="False"
TextWrapping="WrapWithOverflow">
</TextBox>
当长文本添加到绑定变量(LyricsForDisplay)时,列表框中的所有项目都会扩展其文本框/网格宽度,以允许在使用底部出现的滚动条时看到整个字符串...
我想要什么要做的就是使框/网格仅在用户拉伸窗口时调整大小..而不是在输入长文本时调整大小(它可能会环绕..)
有谁知道如何获得该功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
某些东西需要限制文本框可用的水平宽度,在这种情况下,您希望阻止
ListBox
无限期地水平增长:Something needs to contrain the horizontal width available to the TextBoxes, in this case you want to stop the
ListBox
from growing horizontally indefinitely:二改代码:
1-添加边框标签与您需要的 grid.column 和 grid.row 大小。
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:
尝试在文本框中设置 MaxWidth 属性
Try setting the the MaxWidth Property in Your Textbox
不幸的是,常规 TextBox 不允许自动调整大小以适应父级,但当文本不适合时则不允许自动调整大小。
要解决此问题,您可以使用报告所需 (0, 0) 大小的自定义文本框。这是一个丑陋的黑客,但它有效。
在您的 .xaml.cs 文件中:
然后,在您的 .xaml 文件中:
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:
Then, in your .xaml file:
以下内容有效:
请注意
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
和TextWrapping="Wrap"
的使用。The following works:
Notice the use of
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
andTextWrapping="Wrap"
.