【发布时间】:2011-08-10 18:17:14
【问题描述】:
Both of these 问题表示要将拉伸文本框绑定到容器宽度/高度并且不随用户输入而增长,您应该使用占位符边框并绑定到其实际高度/宽度。
不过,这只是一种工作方式。它确实阻止了文本框随着用户输入而增长,但文本框只会调整大小以增长,它永远不会调整大小以缩小。如果您使用 RenderSize,它会增长和缩小,但它会随着用户输入再次增长。此外,添加一个额外的元素来绑定宽度/高度似乎有点 hacky。有没有更好的解决方案?
这似乎应该是拉伸文本框的默认行为。
编辑:这是 XAML(包括下面 Aaron 的建议)
<TabControl HorizontalAlignment="Stretch" Margin="5,15,5,5" Name="tabControl2" VerticalAlignment="Stretch" MinHeight="80">
<TabItem Header="Description" Name="tabItem2" FontSize="14" IsEnabled="True">
<Grid>
<Border Name="b_desc"/>
<TextBox HorizontalAlignment="Stretch" Margin="0" Name="textBox5"
VerticalAlignment="Stretch" FontSize="12" TextWrapping="Wrap"
AutoWordSelection="True" VerticalScrollBarVisibility="Auto"
AcceptsReturn="True"
Width="{Binding ElementName=b_desc, Path=ActualWidth}"
Height="{Binding ElementName=b_desc, Path=ActualHeight}"
MaxWidth="{Binding ElementName=b_desc, Path=Width}"
MaxHeight="{Binding ElementName=b_desc, Path=Height}" />
</Grid>
</TabItem>
</TabControl>
EDIT2:我不确定它是否有所不同,但这些元素是绑定到 ViewModel 集合的 TabControl 的内容。有关该模式的示例,请参见 This Article。
【问题讨论】: