我花了两个小时研究如何避免在输入长文本时我的 WPF TextBox 控件变长,但我无法做到这一点,即使我已经阅读了一些关于它的答案,如下所示:
stopping-wpf-textbox-from-growing-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}
但它对我不起作用。
我还尝试将以下属性添加到网格:
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
或者
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />
但它也不起作用。
我需要我的控件在用户拉伸(stretch)窗口时增长,但在插入长文本时不需要增长。
对于这个问题,您有其他我可以尝试的答案吗?
更新!
我注意到一些非常奇怪的事情:如果我手动拉伸(stretch)窗口,则在插入长文本时文本框会停止增长。没关系。但我需要实现这一点,而不必每次运行程序时都拉伸(stretch)窗口
最佳答案
删除TextBox边框并将其放入ScrollViewer中。
关于wpf - 阻止 WPF 文本框增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627524/