views:

33

answers:

1

How do you set the width of a textbox to fill the container programmatically? For example in xaml if I have the following declaration for the textbox:

<TextBox Height="23"  Name="TextBox1" VerticalAlignment="Top" Width="50" />

how can I set the width so that it fills the container in a button click handler?

+3  A: 

You should be able to use HorizontalAlignment="Stretch" (make sure you don't set an explicit Width or MaxWidth to allow dynamic sizing). I haven't done any Sliverlight development, but in WPF, that does the trick!

Pwninstein
+1 This is correct. This will cause the textbox to be 100% of its container, make sure the container is also stretching if you dont see a change after this. Also, make sure to remove the Width="50" when you set Stretch
David
Another thing to watch out for is the container it's in. In order for the `TextBox` to take up 100% it has to be in a container that has a fixed width. If it's in a `Grid` or `StackPanel` in a vertical orientation it will work, but if the container it's in will expand to fit its content then the `TextBox` will only take up the size necessary to hold its content.
Stephan