首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表框中的WPF文本块未正确裁剪

列表框中的WPF文本块未正确裁剪
EN

Stack Overflow用户
提问于 2010-05-22 02:18:39
回答 2查看 1.5K关注 0票数 2

这是我想要的:一个ListBox,它的项目由一个带有两个TextBlockStackPanel组成。文本块需要支持换行,列表框不应该展开,并且不应该有水平滚动条。这是我到目前为止拥有的代码。将其复制并粘贴到XamlPad中,您将看到我所说的内容:

代码语言:javascript
复制
<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>

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

如何将TextBlock保留在ListBox中,不管有没有垂直滚动条?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-22 05:21:03

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

代码语言:javascript
复制
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
...

然后,您可以删除TextBlocks上的宽度绑定。

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

代码语言:javascript
复制
<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>
票数 3
EN

Stack Overflow用户

发布于 2010-05-22 03:40:52

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

代码语言:javascript
复制
  <ListBox.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Margin" Value="0 0 -6 0" />
        <Setter Property="Padding" Value="0 0 6 0" />
    </Style>
  </ListBox.Resources>

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

代码语言:javascript
复制
<ListBox x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2884507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档