Windows phone 7 TextBlock TextWrapping not listed

I have a list defined as:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox x:Name="myListBox" Width="468" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <toolkit:WrapPanel /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.Template> <ControlTemplate> <ScrollViewer Width="468"> <ItemsPresenter /> </ScrollViewer> </ControlTemplate> </ListBox.Template> </ListBox> </Grid> 

In the code, I create several text blocks as list items with textWrapping enabled in each text block.

  for (int i = 0; i < everyLine.Length; i++) { TextBlock txtBlock = new TextBlock() { TextWrapping = TextWrapping.Wrap, Name = "textBlock" + i, Foreground = textBrush, FontSize = 20, Text = everyLine[i] }; this.myListBox.Items.Add(txtBlock); } 

But not a single text in any of the text blocks is wrapped.

Can someone please let me know if the wrong way to define textBlocks in a list is specified?

+3
source share
2 answers

+1 for Derek's answer

Also, be careful using the <StackPanel> in your ListBox. By default, the ListBox uses <VirtualizingStackPanel> , and this is very important, since when displaying long lists, there are significantly less user interface (memory) resources.

+2
source

Is there any special reason you add elements to your code? From the looks of things, you have a data collection that you can set in the ItemsSource ListBox , and then use the ItemTemplate to specify how each item should look. Something like the following:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox x:Name="myListBox" Width="468"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock FontSize="20" Text="{Binding}" TextWrapping="Wrap" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> 

Note that the default style for the ListBox already includes ScrollViewer , so there is no need to change the ControlTemplate . Since you have already set the width of the ListBox , the above should β€œjust work”.

+2
source

All Articles