Search code examples
silverlight-4.0textblockword-wrap

textwrapping in silverlight 4 not working


I want to do text wrap and show a vertical scroll bar when i have more than a line of text but the text is not getting wrapped at all. this is my xaml

<StackPanel Name="panel">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>

                <StackPanel
                    Grid.Row="0"
                    Grid.Column="0"
                    Orientation="Horizontal"
                    Background="Yellow">

                <TextBlock  Text="Text:" />
                <ScrollViewer                                    
                    BorderThickness="0"
                    Height="33"                                    
                    VerticalScrollBarVisibility="Auto"                                
                    HorizontalScrollBarVisibility="Disabled">                    
                    <TextBlock TextWrapping="Wrap" Text="{Binding Name}"/>
                </ScrollViewer>
                </StackPanel>
            </Grid>
        </StackPanel>

Can someone tell me why the text won't wrap at end of screen ?

Thanks


Solution

  • What is happening here is that your internal StackPanel (and every StackPanel) doesn't constraint its children to the "visible" space in the StackPanel. So, the scrollviewer feels like having unlimited space and so is the textblock. The Grid itself can do it:

    <StackPanel Name="panel">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock  Text="Text:" />
            <ScrollViewer Grid.Column="1"
                                        BorderThickness="0"
                                        Height="33"
                                        VerticalScrollBarVisibility="Auto"
                                        HorizontalScrollBarVisibility="Disabled">
                <TextBlock TextWrapping="Wrap"
                                        Text="YourText" />
            </ScrollViewer>
        </Grid>
    </StackPanel>