I have a StackPanel
in my WinRT C# Metro app that I want to use as container for a label/value pair (TextBlock
and TextBox
) like this:
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Label" Width="150" />
<TextBox Text="Value" />
</StackPanel>
Now what I want is to let the TextBox automatically fill the remaining horizontal space of the StackPanel. Is this possible in any way? HorizontalAlignment
/HorizontalContentAlignment
don't work.
I know that an alternative would be to define a Grid
for that. The problem is that I have this construct several times and want to use it in a Style
definition. I don't want to define the Grid's definition with rows and columns x times...
Perhaps the alternative would be to define a custom user control, but I hoped there would be an easy possibility to get the TextBox
to stretch.
Came across this question while looking up whether a WinRT DockPanel existed.
I ended up slightly modifying the DockPanel that comes with the Silverlight Toolkit, and it seems to work.
To use, add
xmlns:toolkit="using:System.Windows.Controls"
to the top, and taking the question above as the scenario, we could write:DockPanel.cs:
I am not sure which element you put
HorizontalAlignment
to, but this is what works for me:This stretches the
TextBox
to the whole width of theStackPanel
.I walked thru this desert for 10 minutes or hours, and figured out that the best native control to neast the contents is the grid look at my snipet that works fine:
this will generate a nice adaptable layout with a textbox steching and a butoon on it side that maintains his aspect
Unfortunately the alternative (DockPanel) isnt available for Metro. You could try a WrapGrid, but I dont know if it'll solve your problem (Ive never used it).
The only real way of doing this is using a Grid as you described: