Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > TextBox wrapping inside a StackPanel...
질문하기질문하기
 

답변됨TextBox wrapping inside a StackPanel...

  • 2006년 9월 18일 월요일 오전 10:19Ted 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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 ?

답변

  • 2006년 9월 27일 수요일 오전 12:22Dmitry Titov 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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.

     

모든 응답

  • 2006년 9월 18일 월요일 오전 10:18Ted 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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 ?

  • 2006년 9월 27일 수요일 오전 12:22Dmitry Titov 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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.

     

  • 2008년 6월 30일 월요일 오후 7:17dan blanchard 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
     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

    • 편집됨dan blanchard 2008년 6월 30일 월요일 오후 7:19code block not formatted correctly
    •  
  • 2009년 6월 18일 목요일 오후 11:10Sergio_B 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
    <FlowDocumentScrollViewer>
         <FlowDocument>
             <Paragraph TextAlignment="Left">
                 <Run Text="Something:"></Run>
                  <Run Text=":"></Run>
                  <Run x:Name="statusTextBlock" Text="blahblabla blahblablablahblablablahblablablahblablablahblabla" />
              </Paragraph>
         </FlowDocument>
     </FlowDocumentScrollViewer>
    
    Hope this can help