【发布时间】: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