4

I have a data bound Listbox as shown below. I want the textblock that holds the data to wrap. And I have not been able to.

What is the problem here?

Here is my code:

    <DataTemplate x:Key="policyLbTemplate">
        <StackPanel>
            <TextBlock Text="{Binding name}" FontWeight="Bold"/>
            <TextBlock Text="{Binding description}"  TextWrapping="Wrap" />
        </StackPanel>
    </DataTemplate>

 <ListBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Policies}"
                     ItemTemplate="{StaticResource policyLbTemplate}" 
 HorizontalContentAlignment="Stretch" />
double-beep
  • 5,031
  • 17
  • 33
  • 41
Pepper
  • 109
  • 1
  • 7

2 Answers2

5

You need to add one property to the list box: ScrollViewer.HorizontalScrollBarVisibility="Disabled", then the text will wrap (otherwise it grows offscreen). I have done it in the following code and it works fine for me.

<ListBox Name="lstbIcons" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
         VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding UserActionName}" FontWeight="Bold"/>
                <TextBlock Text="{Binding Description}"  TextWrapping="Wrap" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Upendra Chaudhari
  • 6,473
  • 5
  • 25
  • 42
2

This is most likely because there is nothing restricting the width of the TextBlock so that it is growing offscreen. Do Horizontal scrollbars appear?

See the following related questions and try the solutions described:

WP7 TextBlock inside a ListBox not wrapping text

Force TextBlock to wrap in WPF ListBox

windows phone 7 TextBlock TextWrapping not honored in listbox

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/37236ac6-05c3-4acc-baca-abc871ba64e0

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • 1
    Thanks! setting ScrollViewer.HorizontalScrollBarVisibility="Disabled" on the listbox did it. – Pepper Aug 16 '11 at 06:07