Joe.Net
Joe.Net

Reputation: 1215

TextBlock TextWrapping not wrapping inside StackPanel

I have a StackPanel, but the following line :

<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap"  />

is not Wrapping the Text.

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="15" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0" Grid.Column="0">
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
        </DockPanel>
        <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
        <Image
            Grid.Row="0"
            Grid.Column="6"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

        <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap" />

        <Image
            Grid.Row="1"
            Grid.Column="4"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
    </Grid>
</StackPanel>

The StackPanel Orientation is set to 'Vertical' but the TextBlock is not inheriting it.

Where am I going wrong?

Upvotes: 24

Views: 22170

Answers (1)

Adrian Popescu
Adrian Popescu

Reputation: 571

Your problem is using the StackPanel that allows its children to fill in all the available space - the StackPanel stretches accordingly to the size of its content. Try removing the StackPanel and keep just the Grid - this way you will limit the size of its children to the available space used by the Grid.

If that isn't enough in the layout you've built, try setting a MaxWidth on the TextBox that needs wrapping.

Now the source of your problem was also the fact that your TextBox was inserted in the first Column of the Grid that had an infinite size (Width="Auto"). Thus, setting the Grid.Column="7" to the TextBox did the trick you wanted (wrapping text). Here's the revised code:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="15" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Grid.Column="0">
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
    </DockPanel>
    <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
    <Image
        Grid.Row="0"
        Grid.Column="6"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

    <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="7" Text="{Binding Notes}" TextWrapping="Wrap" />

    <Image
        Grid.Row="1"
        Grid.Column="4"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
</Grid>

Upvotes: 28

Related Questions