【问题标题】:WPF Textblock in Listbox not clipping properly列表框中的 WPF 文本块未正确剪辑
【发布时间】:2010-05-21 18:18:38
【问题描述】:

这就是我想要的:一个ListBox,其项目由一个StackPanel 和两个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 中剪切和粘贴)导致垂直滚动条出现,则文本块的宽度不会调整为垂直滚动条。

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

【问题讨论】:

  • 非常好的问题。我倾向于认为这是 WPF TextBlock 中的一个错误。

标签: wpf listbox wpf-controls textblock


【解决方案1】:

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

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

【讨论】:

  • 第二个效果很好!即使我使用显示设置使滚动条变宽,它也可以工作。谢谢!
【解决方案2】:

你可以这样解决这个问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多