I have a ListBox that contains TextBlocks.
Sometimes the content of a TextBlock is too long and I want the height of this entry to double or triple as needed to accommodate the text.
I tried TextWrapping="Wrap" but it doesn't work. Every TextBlock is still just one line in height.
Is there an easy way to fix the problem in XAML? Thanks.
* Additional info: I tried to simplify the question but perhaps the complete scenario is better.
- I have a listbox whose entries are displayed according to a template in code below.
- Each entry has 2 pieces of info: a product price followed by product name.
- I don't want to use the horizontal scrollbar in the listbox and want the product name to be displayed in 2 or more lines if necessary. The product name is the 2nd TextBlock.
Here's my XAML:
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock MinWidth="40" TextAlignment="Right" Text = "{Binding ProductPrice}" />
<TextBlock Text = "{Binding ProductName}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Best Answer
Disable the listbox horizontal
scrollViewer
. This way thetextBlock
will be forced to wrap.XAML:
Example result:
Edit:
From the XAML you added I'm confident that the problem lays in the
StackPanel
. Try replacing it withGrid
for example:StackPanel
doesn't limit the content size, therefore thetextBlock
doesn't knows where the space ends , and the wrapping doesn't happens.