19

我有一个与拉伸到它的窗口对齐的TextBlock内。ScrollViewer我需要TextBlock表现如下:

  • 用窗口调整大小,没有滚动条
  • 当调整到某个宽度以下时 ,应该出现保留 a 和滚动条的TextBlock需要MinWidth
  • TextWrapping或者TextTrimming应该正常工作

我怎样才能获得这个功能?

我尝试了几种方法,涉及到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>
4

2 回答 2

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>
于 2009-12-30T19:56:16.500 回答
2

没有更多细节,我能做的最好的就是提供这样做的标准方法。基本上,在滚动查看器中托管您的元素(具有最小尺寸);当滚动查看器被调整到足够小以至于元素不能完全放入其中时,它将自动显示滚动条。例子:

<ScrollViewer>
    <Button MinWidth="100" MinHeight="50"/>
</ScrollViewer>
于 2009-12-30T17:03:01.873 回答