我花了两个小时研究如何避免在输入长文本时我的 WPF TextBox 控件变长,但我无法做到这一点,即使我读过一些关于它的答案,如下所示:
停止 wpf-textbox-from-grow-with-text https://stackoverflow.com/questions/3720855/stopping-wpf-textbox-from-growing-with-text
wpf-允许文本框调整大小但不根据用户输入增长 https://stackoverflow.com/questions/604952/wpf-allow-textbox-to-be-resized-but-not-to-grow-on-user-input
wpf 如何停止文本框自动调整大小 https://stackoverflow.com/questions/4465646/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"
Or
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />
但它也不起作用。
我需要我的控件在用户拉伸窗口时增长,但在插入长文本时不需要增长。
对于这个问题,您有我可以尝试的不同答案吗?
UPDATE!
我注意到一些非常奇怪的事情:如果我手动拉伸窗口,则在插入长文本时文本框会停止增长。没关系。但我需要实现这一目标,而不必每次运行程序时都拉伸窗口