provençal le breton
provençal le breton

Reputation: 1438

StackPanel Orientation in xaml

I got a weird problem on my xaml, in a stackPanel.

My stackPanel contains a textbox, and a button.

This should be on the same line (if possible, depending on the text width). The problem is :

if the stackPanel have Orientation="Vertical", the button will go to the line bellow the text. if the stackPanel have Orientation="Horizontal" , the line will not doing any break line, so all the line will go out of my grid.

<StackPanel Name="spRemplir"
            Grid.Row="2"
            Grid.ColumnSpan="6"
            Width="560"
            Margin="5,5,5,5"
            Orientation="Horizontal"
            VerticalAlignment="Center">
 <TextBlock FontWeight="Bold"
            Text={Binding Text}
            TextWrapping="Wrap"/>
 <Button Name="btRemplir"
         Margin="5,0,0,0"
         Width="150"
         Content="Remplir"/>
</StackPanel>

How can I obtain a stackPanel, that will break lines if necessary, and have a text and a button on the same line?

Update with Wrapanel thanks to Eli Arbel :

 <toolkit:WrapPanel  Name="spRemplir"
                     Grid.Row="2"
                     Grid.ColumnSpan="6"
                     Margin="5,5,5,5"
                     Width="560"
                     Orientation="Horizontal">
   <TextBlock FontWeight="Bold"
              Text={Binding Text}
              TextWrapping="Wrap"/>
   <Button Name="btRemplir"
           Content="Remplir"/>
</toolkit:WrapPanel>

But, the button still on the next line, while there is enough space after the text on the same line.

Itried removing the Width on the panel, but then, there is no more wrapping...

I don't understand. Even if there is stackPanel on the same Grid, they should not disturb the wrap panel right?

Thank you.

Upvotes: 0

Views: 586

Answers (3)

Greg Gum
Greg Gum

Reputation: 37875

Try using a Grid instead of a StackPanel. The problem with StackPanel is that they do not report up the Visual Tree that they are out of room. This is not a bug, it's just the way they are and it's appropriate when you need it. But I avoid them except on the innermost elements. But don't have StackPanels in StackPanels as you will lose TextWrapping/Scrolling and just have elements fall of the right or bottom of the page.

Second, make sure that your outer container is Set so that the width is Constrained. For example, in your layout root, give it one Column, and set the Width for * which means = "The available space, but not more"

Once you have your outer container constrained, then your TextBlock will wrap properly.

Greg

Upvotes: 1

Eli Arbel
Eli Arbel

Reputation: 22739

You can try a WrapPanel from the Silverlight Toolkit. Note this panel will not allow you to stretch the items to the container width.

You should also look into removing the fixed Width of the panel.

Upvotes: 2

Demir
Demir

Reputation: 1837

Consider using a WrapPanel instead stack panel: http://wpftutorial.net/WrapPanel.html

Upvotes: 0

Related Questions