以下是我想要的内容:ListBox
,其项目由StackPanel
和TextBlock
个<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>
组成。文本块需要支持包装,列表框不应该展开,并且不应该有水平滚动条。这是我到目前为止的代码。将其复制并粘贴到XamlPad中,您就会看到我在说什么:
TextBlock
这似乎是在保持文本块不增长的工作,但是有一个问题。文本块似乎略大于列表框,导致出现水平滚动条。这很奇怪,因为它们的宽度绑定到lisbox的ActualWidth。此外,如果您向列表框中添加更多项目(仅在XamlPad中剪切并粘贴)导致垂直滚动条出现,则文本块的宽度不会调整为垂直滚动条。
如果有或没有垂直滚动条,如何将ListBox
保留在{{1}}内?
答案 0 :(得分: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>
答案 1 :(得分: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>
如果fontsize发生变化,这可能效果不佳。 另一种(可能更好)的方法是完全禁用滚动条:
<ListBox x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled">