阻止 WPF TextBox 增长 [英] Stop WPF TextBox from growing
问题描述
我花了两个小时研究如何避免在输入长文本时我的 WPF TextBox 控件增长,但我无法做到,即使我已经阅读了一些关于它的答案,例如:
I have spent two hours researching how to avoid that my WPF TextBox Control grows when a long text has been typed in, but I have not been able to do it, even I have read some answers about it like these ones:
stopping-wpf-textbox-from-growth-with-text一个>
wpf-allow-textbox-to-be-resized-but-not-to-grow-on-user-input
wpf-how-to-stop-textbox-from-autosizing一个>
我的代码如下:
<Grid>
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True"/>
<CheckBox Content="Case sensitive" HorizontalAlignment="Left" Margin="7,0,0,2" Name="checkBox1" Height="16" VerticalAlignment="Bottom" />
</Grid>
我尝试过的一件事是:
MaxWidth={Binding ElementName=MyGrid, Path=ActualWidth}
但它对我不起作用.
我还尝试将以下属性添加到网格:
I also tried to add the following property to the Grid:
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
或
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />
但也没有用.
我需要我的控件在用户拉伸窗口时增长,而不是在插入长文本时增长.
I need my control to grow when the user stretches the window, but not to when a long text is inserted.
你对这个问题有什么不同的答案,我可以试试吗?
Do you have a different answer for this problem that I might try?
更新!
我注意到一些很奇怪的事情:如果我手动拉伸窗口,插入长文本时文本框会停止增长.没关系.但我需要实现这一点,而不必每次运行程序时都拉伸窗口
I have noticed something very strange: If I stretch manually the window, the textbox stops growing when a long text is inserted. That's fine. But I need to achieve this without having to stretch the window every time I run the program
推荐答案
尝试做下一个:
<ScrollViewer MaxHeight={Binding ElementName=MyGrid, Path=ActualWidth} BorderThickness="0">
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" AcceptsReturn="True"/>
</ScrollViewer>
这篇关于阻止 WPF TextBox 增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!