C# WPF - ScrollViewer +文本块的麻烦
我在 ScrollViewer
中有一个 TextBlock
,它与窗口的拉伸对齐。我需要 TextBlock
的行为如下:
- 随窗口调整大小,无滚动条
- 当调整大小低于特定宽度时,
TextBlock
需要保持MinWidth
并且滚动条应该出现 TextWrapping
或TextTrimming
应该正常工作
如何获得此功能?
我尝试了多种方法,包括绑定到 ActualWidth 和 ActualWidth 。 ActualHeight
,但无法让它工作。
这不可能那么困难,我错过了什么?
以下是放入 XamlPad 中的代码示例(尚未设置 MinWidth):
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBlock TextWrapping="Wrap" Text="Some really long text that should probably wordwrap when you resize the window." />
</ScrollViewer>
</Window>
I have a TextBlock
within a ScrollViewer
that aligns with stretch to its window. I need the TextBlock
to behave as the following:
- Resizes with window, no scrollbars
- When resized below a certain width the
TextBlock
needs to keep aMinWidth
and scrollbars should appear TextWrapping
orTextTrimming
should work appropriately
How can I get this functionality?
I have tried several ways, involving bindings to ActualWidth
& ActualHeight
, but can't get it to work.
This can't be that difficult, what am I missing?
Here is a code sample to put in XamlPad (no MinWidth is set yet):
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBlock TextWrapping="Wrap" Text="Some really long text that should probably wordwrap when you resize the window." />
</ScrollViewer>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效:
This works:
如果没有更多细节,我能做的就是提供执行此操作的标准方法。基本上,将您的元素(具有最小尺寸)托管在滚动查看器中;当滚动查看器的大小调整得足够小,以致元素无法完全容纳在其中时,它将自动显示滚动条。例子:
Without more detail, the best I can do is provide the standard way of doing this. Basically, host your element (which has a minimum size) in a scroll viewer; when the scrollviewer is resized small enough such that the element cannot wholly fit inside it, it will automatically display scroll bars. Example: