Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 TextBlock inside a ListBox not wrapping text

I have a ListBox which has StackPanels holding a TextBlock and an Image horizontally, followed by a ContentPresenter. This is what the XAML looks like:

<Grid x:Name="ContentPanel"
      Grid.Row="1"
      Margin="12,0,12,0">
  <ListBox x:Name="MainListBox"
           Margin="12,0,12,0"
           SelectionChanged="MainListBox_SelectionChanged">
    <ListBox.ItemTemplate>
      <DataTemplate>

        <StackPanel>
          <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu x:Name="ContextMenu"
                                 Opened="ContextMenu_Opened">
              <toolkit:MenuItem Header="edit"
                                Tag="edit"
                                Click="MenuItem_Click" />
              <toolkit:MenuItem Header="delete"
                                Tag="delete"
                                Click="MenuItem_Click" />
            </toolkit:ContextMenu>
          </toolkit:ContextMenuService.ContextMenu>
          <StackPanel Orientation="Horizontal"
                      HorizontalAlignment="Left">

            <!-- **** This text won't wrap **** -->
            <TextBlock Text="{Binding Header}"
                       TextWrapping="Wrap"
                       Style="{StaticResource PhoneTextNormalStyle}"
                       Foreground="{StaticResource PhoneAccentBrush}" />

            <Image Source="/image.png"
                   Visibility="{Binding ImageVisibility}" />

          </StackPanel>

          <ContentPresenter Content="{Binding Content}"
                            HorizontalAlignment="Stretch" />

        </StackPanel>

      </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</Grid>

I'm setting the ItemsSource of the ListBox to an ObservableCollection within the page constructor. Everything works fine until the Header text becomes too long, in which case it is not wrapping as I've specified it to. How can I force the TextBlock to wrap the text?

Thanks for your help!

like image 954
Praetorian Avatar asked Jan 02 '11 04:01

Praetorian


1 Answers

This is likely a result of not restricting the width of the TextBlock, so it is growing horizontaly off screen where you can't see it.

like image 127
Mick N Avatar answered Nov 15 '22 11:11

Mick N