0

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">
                            <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.

Community
  • 1
  • 1
Kyeotic
  • 19,697
  • 10
  • 71
  • 128

1 Answers1

1

Bind the MaxWidth property of the TextBox to the Width property of the container.

<TextBox Width="Auto" MaxWidth="{Binding ElementName=myTabControl, Path=Width}"/>
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • @Tyrsius I must be missing something; the XAML you posted works as expected. If the container grows, the TextBox grows, it also shrinks as the container shrinks. It also does not grow with user input. – Aaron McIver Aug 10 '11 at 18:30
  • The only other thing I can think of is that the whole thing is in a tabcontrol of usercontrols. Could the window shrinking not propagate down the same way growing does? – Kyeotic Aug 10 '11 at 18:44
  • @Tyrsius Take your code above and place it in a Grid; does it work as expected? – Aaron McIver Aug 10 '11 at 18:48
  • It already is in a grid. I don't want to post the whole thing though, the rest of the grid has a lot of other contents. – Kyeotic Aug 10 '11 at 18:50
  • @Tyrsius I am trying to get you to remove outer containers; to see if the desired behavior is what you are after as it appears to be when I tossed together a quick app to verify this. – Aaron McIver Aug 10 '11 at 19:21
  • Yes, it seems to work without any other containers. I still need to figure out why it isn't working inside the container its in. Is there a limit to the number of nested containers for this layout rule? – Kyeotic Aug 10 '11 at 20:06
  • I discovered the problem. Elsewhere on the view is a scrollviewer, removing it fixes the issue. I will need to figure out a way to make them both work together. – Kyeotic Aug 10 '11 at 20:11
  • @Tyrsius The ScrollViewer will allow the width to expand indefinitely; without a better understanding of the layout a new question may be in order. – Aaron McIver Aug 10 '11 at 20:40
  • I agree. I marked your answer as correct. I will need to experiment more before I ask about the scrollviewer. – Kyeotic Aug 10 '11 at 20:58