2

这就是我想要的:A ListBox,其项目由 aStackPanel和两个TextBlocks 组成。文本块需要支持换行,列表框不应该展开,并且不应该有水平滚动条。这是我到目前为止的代码。将其复制并粘贴到 XamlPad 中,您将看到我在说什么:

<ListBox Height="300" Width="300" x:Name="tvShows">
    <ListBox.Items>
        <ListBoxItem>
            <StackPanel>
                <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
                <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
            </StackPanel>
        </ListBoxItem>
        <ListBoxItem>
            <StackPanel>
                <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
                <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
            </StackPanel>
        </ListBoxItem>
    </ListBox.Items>
</ListBox>

这似乎可以防止文本块增长,但有一个问题。文本块似乎比列表框稍大,导致出现水平滚动条。这很奇怪,因为它们的宽度绑定到 lisbox 的 ActualWidth。此外,如果向列表框添加更多项目(只是在 XamlPad 中剪切和粘贴)导致垂直滚动条出现,则文本块的宽度不会调整为垂直滚动条。

如何将 s 保留在TextBlocks 中ListBox,有或没有垂直滚动条?

4

2 回答 2

3

有两种方法可以做到这一点,但我认为你真正想要的是禁用水平滚动条,这是通过附加属性完成的:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
...

然后,您可以删除TextBlocks.

您的另一个选择是将TextBlocks宽度绑定到ScrollContentPresenter's ActualWidth通过RelativeSource绑定:

<ListBox Height="300" Width="300" x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.Items>
        <ListBoxItem>
            <StackPanel>
                <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
                <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
            </StackPanel>
        </ListBoxItem>
        <ListBoxItem>
            <StackPanel>
                <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
                <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock>
            </StackPanel>
        </ListBoxItem>
    </ListBox.Items>
</ListBox>
于 2010-05-21T21:21:02.570 回答
0

您可以像这样解决问题:

  <ListBox.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Margin" Value="0 0 -6 0" />
        <Setter Property="Padding" Value="0 0 6 0" />
    </Style>
  </ListBox.Resources>

如果字体大小发生变化,这可能无法正常工作。另一种(可能更好)的方法是完全禁用滚动条:

<ListBox x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
于 2010-05-21T19:40:51.853 回答