ListBox内的WP7 TextBlock不换行 [英] WP7 TextBlock inside a ListBox not wrapping text
问题描述
我有一个 ListBox
,它的 StackPanel
s 水平放置一个 TextBlock
和一个 Image
,然后是一个ContentPresenter
.这就是 XAML 的样子:
I have a ListBox
which has StackPanel
s holding a TextBlock
and an Image
horizontally, followed by a ContentPresenter
. This is what the XAML looks like:
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<ListBox x:Name="MainListBox"
Margin="12,0,12,0"
SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="ContextMenu"
Opened="ContextMenu_Opened">
<toolkit:MenuItem Header="edit"
Tag="edit"
Click="MenuItem_Click" />
<toolkit:MenuItem Header="delete"
Tag="delete"
Click="MenuItem_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Left">
<!-- **** This text won't wrap **** -->
<TextBlock Text="{Binding Header}"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextNormalStyle}"
Foreground="{StaticResource PhoneAccentBrush}" />
<Image Source="/image.png"
Visibility="{Binding ImageVisibility}" />
</StackPanel>
<ContentPresenter Content="{Binding Content}"
HorizontalAlignment="Stretch" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
我将 ListBox
的 ItemsSource
设置为页面构造函数中的 ObservableCollection
.一切正常,直到 Header
文本变得太长,在这种情况下,它不会像我指定的那样换行.如何强制 TextBlock
换行?
I'm setting the ItemsSource
of the ListBox
to an ObservableCollection
within the page constructor. Everything works fine until the Header
text becomes too long, in which case it is not wrapping as I've specified it to. How can I force the TextBlock
to wrap the text?
感谢您的帮助!
推荐答案
这可能是因为没有限制 TextBlock 的宽度,所以它在你看不到的屏幕上水平增长.
This is likely a result of not restricting the width of the TextBlock, so it is growing horizontaly off screen where you can't see it.
这篇关于ListBox内的WP7 TextBlock不换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!