Develobeer
Develobeer

Reputation: 435

Text Wrap ListBox on Windows Phone 8

How can I wrap a long text in a ListBox without using of TextBlock. My code is as follows:

public Sample()
        {
            InitializeComponent();
            quotes.Add("LooooooooooooooooooooooooooongTeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext");
            myListbox.DataContext = quotes;

}

quotes is a list.

XAML:

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" HorizontalScrollBarVisibility="Disabled">
            <Grid x:Name="ContentPanel" Margin="12,0,12,0" Grid.Column="1">
                <ListBox x:Name="myListbox" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="600" ItemsSource="{Binding}" Margin="10,4,10,3">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>

                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>

                 </Grid>
        </ScrollViewer>

I have also tried with WrapPanel, but doesn't work.

Upvotes: 1

Views: 1170

Answers (1)

har07
har07

Reputation: 89325

ItemsPanel controls how ListBox arrange ListBoxItems in certain Layout. Try to define ItemTemplate instead of ItemsPanel, ItemTemplate controls how data in each item displayed, including whether it wrapped or not. Sorry, but I am using TextBlock here, because I can't see why avoid TextBlock. Its even very likely (haven't proved this as for now) that TextBlock is the default ItemTemplate for ListBox.

<ListBox x:Name="myListbox" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="600" ItemsSource="{Binding}" Margin="10,4,10,3">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 1

Related Questions