C# WPF - ScrollViewer +文本块的麻烦

发布于 2024-08-16 01:29:26 字数 905 浏览 2 评论 0原文

我在 ScrollViewer 中有一个 TextBlock,它与窗口的拉伸对齐。我需要 TextBlock 的行为如下:

  • 随窗口调整大小,无滚动条
  • 当调整大小低于特定宽度时,TextBlock 需要保持 MinWidth并且滚动条应该出现
  • TextWrappingTextTrimming 应该正常工作

如何获得此功能?

我尝试了多种方法,包括绑定到 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 a MinWidth and scrollbars should appear
  • TextWrapping or TextTrimming 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-08-23 01:29:26

这有效:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" 
                  VerticalScrollBarVisibility="Auto"
                  Name="Scroller">
        <TextBlock HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   MinWidth="100"
                   Width="{Binding ElementName=Scroller, Path=ViewportWidth}"
                   TextWrapping="Wrap"
                   Text="Some really long text that should probably wordwrap when you resize the window." />
    </ScrollViewer>
</Window>

This works:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" 
                  VerticalScrollBarVisibility="Auto"
                  Name="Scroller">
        <TextBlock HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   MinWidth="100"
                   Width="{Binding ElementName=Scroller, Path=ViewportWidth}"
                   TextWrapping="Wrap"
                   Text="Some really long text that should probably wordwrap when you resize the window." />
    </ScrollViewer>
</Window>
飞烟轻若梦 2024-08-23 01:29:26

如果没有更多细节,我能做的就是提供执行此操作的标准方法。基本上,将您的元素(具有最小尺寸)托管在滚动查看器中;当滚动查看器的大小调整得足够小,以致元素无法完全容纳在其中时,它将自动显示滚动条。例子:

<ScrollViewer>
    <Button MinWidth="100" MinHeight="50"/>
</ScrollViewer>

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:

<ScrollViewer>
    <Button MinWidth="100" MinHeight="50"/>
</ScrollViewer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文