这是我想要的:一个ListBox,它的项目由一个带有两个TextBlock的StackPanel组成。文本块需要支持换行,列表框不应该展开,并且不应该有水平滚动条。这是我到目前为止拥有的代码。将其复制并粘贴到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>这似乎起到了阻止文本块增长的作用,但有一个问题。文本块似乎比列表框稍大,导致出现水平滚动条。这很奇怪,因为它们的宽度与列表框的ActualWidth绑定在一起。此外,如果您在列表框中添加了更多项(只需在XamlPad中剪切和粘贴),则会出现垂直滚动条,文本块的宽度不会调整为垂直滚动条的大小。
如何将TextBlock保留在ListBox中,不管有没有垂直滚动条?
发布于 2010-05-22 05:21:03
有两种方法可以做到这一点,但我认为你真正想要的是禁用水平滚动条,这是通过附加属性来完成的:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
...然后,您可以删除TextBlocks上的宽度绑定。
您的另一个选择是通过RelativeSource绑定将TextBlocks宽度绑定到ScrollContentPresenter's ActualWidth:
<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-22 03:40:52
你可以像这样解决这个问题:
<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">https://stackoverflow.com/questions/2884507
复制相似问题