Automatic text wrapping in listbox

I have a DataTemplate for my ListBox and want to wrap a TextBlock, so the message will be displayed in the next line... So I've written this code:

<ListBox x:Name="CareListBox" ItemsSource="{Binding}" Grid.Column="1" Background="Transparent">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <Image Source="pic.png" />
        <TextBlock Text="{Binding Message}" Style="{StaticResource SubheaderTextStyle}" Margin="25,0,0,0" HorizontalAlignment="Stretch" TextWrapping="Wrap"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

What am I doing wrong?

Upvotes: 0

Views: 1266

Answers (1)

devdigital
devdigital

Reputation: 34369

I would use a Grid with two columns rather than a horizontal StackPanel as the StackPanel won't have a width constraint.

Upvotes: 1

Related Questions