ListBox 内的 WP7 TextBlock 不换行文本

发布于 2024-10-09 19:58:49 字数 2297 浏览 2 评论 0原文

我有一个 ListBox,其中有 StackPanel 水平放置一个 TextBlock 和一个 Image,后面跟着一个 内容演示者。 XAML 如下所示:

<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>

我将 ListBoxItemsSource 设置为页面构造函数中的 ObservableCollection。一切正常,直到 Header 文本变得太长,在这种情况下,它不会像我指定的那样换行。如何强制 TextBlock 换行文本?

感谢您的帮助!

I have a ListBox which has StackPanels 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>

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?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-10-16 19:58:49

这可能是由于不限制 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文