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. Also, adding an additional element to bind width/height to seems a bit 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)
<TabControl HorizontalAlignment="Stretch" Margin="5,15,5,5" Name="tabControl2" VerticalAlignment="Stretch" MinHeight="80">
开发者_如何学Python <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. See This Article for an example of the pattern.
Bind the MaxWidth
property of the TextBox
to the Width
property of the container.
<TextBox Width="Auto" MaxWidth="{Binding ElementName=myTabControl, Path=Width}"/>
精彩评论