简体   繁体   English

WPF 布局:文本框在 Tabcontrol 内增长

[英]WPF Layout: Textbox growing inside Tabcontrol

Both of these questions say that to bind a stretching textbox to container width/height and not grow with user input, you should user a placeholder border and bind to its actual height/width. 这两个问题都说要将拉伸文本框绑定到容器宽度/高度并且不随用户输入而增长,您应该使用占位符边框并绑定到其实际高度/宽度。

This only sort of works though.不过,这只是一种工作方式。 It does stop the textbox from growing with user input, but the textbox will only resize to grow, it will never resize to shrink.它确实会阻止文本框随着用户输入而增长,但文本框只会调整大小以增长,永远不会调整大小以缩小。 If you use RenderSize it will grow and shrink, but it will grow with user input again.如果您使用 RenderSize,它会增长和缩小,但它会随着用户输入再次增长。 Also, adding an additional element to bind width/height to seems a bit hacky.此外,添加一个额外的元素来绑定宽度/高度似乎有点 hacky。 Is there a better solution?有更好的解决方案吗?

This seems like it should be the default behavior of stretching textboxes.这似乎应该是拉伸文本框的默认行为。

EDIT : Here is the XAML (including Aaron's suggestion below)编辑:这是 XAML (包括下面亚伦的建议)

<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 : I am not sure if it makes a difference, but these elements are the content of a TabControl bound to a collection of ViewModels. EDIT2 :我不确定它是否有所作为,但这些元素是绑定到 ViewModel 集合的 TabControl 的内容。 See This Article for an example of the pattern.有关该模式的示例,请参阅本文

Bind the MaxWidth property of the TextBox to the Width property of the container.TextBoxMaxWidth属性绑定到容器的Width属性。

<TextBox Width="Auto" MaxWidth="{Binding ElementName=myTabControl, Path=Width}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM