无法在 WPF TextBlock 中获取垂直滚动条
我正在 wpf TextBlock 控件(.Net 3.5)中呈现文本。文本块的内容根据用户在列表框中选择的内容而变化。文本换行,所以我不需要水平滚动条。但是,文本通常多于窗口可以显示的数量,因此我需要一个垂直滚动条。
当我开始搜索时,我很快发现答案是将 TextBlock 包装在 ScrollViewer 中。然而,它不起作用(TM),我希望有人能帮助我找出原因。
这是 UI 代码的结构:
<Window x:Class=..>
<StackPanel>
<ListBox HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</Window>
当用户在列表框中选择一个项目时,与该项目关联的一些文本将显示在 TextBlock 中。我本以为现在的代码应该是所需要的,但它从来没有为我提供滚动条。
搜索和实验给了我两条线索:问题的根源可能与我动态更新 TextBlock 的内容有关,并且 TextBlock 不会根据新内容调整自身大小。我发现一个似乎相关的帖子说,通过将 TextBlock 的高度设置为其 ActualHeight(更改其内容后),它就可以工作。但事实并非如此(我看不出这有什么影响)。
其次,如果我设置 ScrollViewer 的高度(在设计时),那么我确实会得到一个垂直滚动条。例如,如果我在上面的 xaml 中将其设置为 300,则结果几乎很好,因为当(且仅当)我需要它时,第一次打开的窗口包含一个带有垂直滚动条的 TextBlock。但是,如果我使窗口变大(在运行时用鼠标调整其大小),ScrollViewer 不会利用新的窗口大小,而是根据 xaml 保持其高度,这当然是行不通的。
希望我只是忽略了一些明显的事情......
谢谢!
I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the textblock varies depending on what the user selects in a list box. The text wraps, so I don't need an horizontal scroll bar. However, there is often more text than the amount the window can display, so I need a vertical scroll bar.
As I started searching I quickly found that the answer is to wrap the TextBlock in a ScrollViewer. However, It Does Not Work (TM) and I'm hoping someone can help me work out why.
This is the structure of the UI code:
<Window x:Class=..>
<StackPanel>
<ListBox HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</Window>
When the user selects an item in the list box, some text associated with this item is presented in the TextBlock. I would have thought that the code as it stands should have been all that's required, but it never provides me with a scroll bar.
Searching and experimenting have given me two clues: the root of the problem might be related to me updating the content of the TextBlock dynamically, and that the TextBlock does not resize itself based on the new content. I found a posting that seemed relevant that said that by setting the Height of the TextBlock to its ActualHeight (after having changed its content), it would work. But it didn't (I can see no effect of this).
Second, if I set the height (during design time) of the ScrollViewer, then I do get a vertical scroll bar. For instance, if I set it to 300 in the xaml above, the result is almost good in that the window as first opened contains a TextBlock with a vertical scroll bar when (and only when) I need it. But if I make the window larger (resizing it with the mouse during runtime), the ScrollViewer does not exploit the new window size and instead keeps its height as per the xaml which of course won't do.
Hopefully, I've just overlooked something obvious..
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您的 ScrollViewer 位于 StackPanel 中,所以它将被给予显示其内容所需的尽可能多的垂直空间。
您需要使用限制垂直空间的父面板,例如 DockPanel 或 Grid。
Because your ScrollViewer is in a StackPanel it will be given as much vertical space as it needs to display it's content.
You would need to use a parent panel that restricts the vertical space, like DockPanel or Grid.