wpf - 阻止 WPF 文本框增长

标签 wpf textbox

我花了两个小时研究如何避免在输入长文本时我的 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/

相关文章:

c# - IsChecked 绑定(bind)在 ContextMenu 的 MenuItem 中不起作用

Windows 8 Metro - 如何制作密码文本框?

c# - 00 :00 Time Format > 24 的正则表达式

c# - toString保留小数位

.net - 在 XAML 中导入命名空间时会发生什么?

c# wpf datagrid column width auto size 如果需要添加水平滚动

c# - 文本框显示格式

html - 为 html5 输入类型 ="date"呈现 asp.TextBox

wpf - Kinect SDK 1.6 和 Joint.ScaleTo 方法

wpf - 使用 WPF 和 MVVM 创建动态行和列