gpt4 book ai didi

列表框中的 WPF 文本 block 未正确剪辑

转载 作者:行者123 更新时间:2023-12-04 06:52:45 24 4
gpt4 key购买 nike

这就是我想要的:A ListBox其项目由 StackPanel 组成有两个 TextBlock s。文本 block 需要支持换行,列表框不应该展开,并且不应该有水平滚动条。这是我到目前为止的代码。将其复制并粘贴到 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>

这似乎可以防止文本 block 增长,但有一个问题。文本 block 似乎比列表框稍大,导致出现水平滚动条。这很奇怪,因为它们的宽度绑定(bind)到 lisbox 的 ActualWidth。此外,如果向列表框添加更多项目(只是在 XamlPad 中剪切和粘贴)导致垂直滚动条出现,则文本 block 的宽度不会调整为垂直滚动条。

如何保留 TextBlockListBox 内,有或没有垂直滚动条?

最佳答案

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

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
...

然后您可以删除 TextBlocks 上的宽度绑定(bind)。 .

您的另一个选择是绑定(bind) TextBlocks宽度为 ScrollContentPresenter's ActualWidth通过 RelativeSource绑定(bind):
<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>

关于列表框中的 WPF 文本 block 未正确剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com