TextBox wrapping inside a StackPanel...
I just trying to find out why wrapping of a text inside a textbox isn't working inside a horizontal stackpanel, but is working inside a vertical stackpanel (or a grid). The following doesn't wrap the text:
----------
<
Window x:Class="WPFSizingTest.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFSizingTest" Height="300" Width="300"><
StackPanel Orientation="Horizontal" HorizontalAlignment="Left"><Button VerticalAlignment="Top">X</Button>
<TextBlock TextWrapping="Wrap" FontSize="25" Padding="3">This is some long text that should be wrapped with a larger font.</TextBlock>
</StackPanel>
</Window>
----------
But the following does wrap the text...
----------
<
Window x:Class="WPFSizingTest.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFSizingTest" Height="300" Width="300"><
Grid><
Grid.ColumnDefinitions><
ColumnDefinition Width="Auto"/><
ColumnDefinition Width="*"/></
Grid.ColumnDefinitions><
Button Grid.Row="0" Grid.Column="0" VerticalAlignment="Top">X</Button><
TextBlock Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" FontSize="25" Padding="3">This is some long text that should be wrapped with a larger font.</TextBlock></
Grid></
Window>----------
Can anyone explain why ?
Answers
Panels generally take "availableSize" - the space initially provided bythe window of the application and go recursively down the element tree, partitioning this available size and measuring children. Children in response produce "desired size" - the one they woudl like to occupy.
In this process, layout panels treat “availableSize” in one of 2 ways – they either are “constrained” in a particular dimension (size children to available size) or unconstrained – size children to child's DesiredSize. The latter behavior is also referred as “size to content”. When a direction is treated as "unconstrained", the child is asked to measure itself given "infinite" available size. Text, when unconstrained, will not wrap and rather occupy a single line.
Same panel may treat one dimension as constrained while another as “size to content”. Here are some panels and how they treat their children:
X dimension Y dimension
Canvas to content to content
Dock constrained constrained
StackPanel (vertical) constrained to content
StackPanel (horizontal) to content constrained
Grid constrained constrained ß except “Auto” rows and columns
WrapPanel to content to content“Stretch” alignment is only applicable in the dimension that is constrained.
In your example, horizontal StackPanel sizes to available size in vertical dimension, but gives children infinite width as "available size and then arranges them at their desired size - which in case of text is the width of a single line.
All Replies
I just trying to find out why wrapping of a text inside a textbox isn't working inside a horizontal stackpanel, but is working inside a vertical stackpanel (or a grid). The following doesn't wrap the text:
----------
<
Window x:Class="WPFSizingTest.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFSizingTest" Height="300" Width="300"><
StackPanel Orientation="Horizontal" HorizontalAlignment="Left"><Button VerticalAlignment="Top">X</Button>
<TextBlock TextWrapping="Wrap" FontSize="25" Padding="3">This is some long text that should be wrapped with a larger font.</TextBlock>
</StackPanel>
</Window>
----------
But the following does wrap the text...
----------
<
Window x:Class="WPFSizingTest.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFSizingTest" Height="300" Width="300"><
Grid><
Grid.ColumnDefinitions><
ColumnDefinition Width="Auto"/><
ColumnDefinition Width="*"/></
Grid.ColumnDefinitions><
Button Grid.Row="0" Grid.Column="0" VerticalAlignment="Top">X</Button><
TextBlock Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" FontSize="25" Padding="3">This is some long text that should be wrapped with a larger font.</TextBlock></
Grid></
Window>----------
Can anyone explain why ?
- Hi,
I am having a difficult time with this - I have three textblocks that need to go next to each other horizontally and then the last one should wrap below it, like
Something: blah blah blah this goes really long and should wrap on the next line directly under Something and keep going and going
<StackPanel Orientation="Horizontal" > <TextBlock Style="{StaticResource HumanWhite14PointBoldTextStyle}" Text="Something:" /> <TextBlock Style="{StaticResource HumanWhite14PointBoldTextStyle}" Text=":" /> <TextBlock x:Name="statusTextBlock" Margin="5,0,0,0" Style="{StaticResource HumanWhite14PointTextStyle}" Text="Very very long strong"/> </StackPanel>
The StackPanel as Horizontal doesn't wrap, I could wrap it in a grid the long string would be in it's own column and not under the Something: , in a wrappanel, if it needs to wrap it will put the beginning of the long string on the line below it... So, how can I do this?
Thanks
Dan- Edited bydan blanchard Monday, June 30, 2008 7:19 PMcode block not formatted correctly
- Hope this can help
<FlowDocumentScrollViewer> <FlowDocument> <Paragraph TextAlignment="Left"> <Run Text="Something:"></Run> <Run Text=":"></Run> <Run x:Name="statusTextBlock" Text="blahblabla blahblablablahblablablahblablablahblablablahblabla" /> </Paragraph> </FlowDocument> </FlowDocumentScrollViewer>

