C# WPF - ScrollViewer + TextBlock troubles

后端 未结 3 1221
迷失自我
迷失自我 2020-12-31 02:17

I have a TextBlock within a ScrollViewer that aligns with stretch to its window. I need the TextBlock to behave as the following:

相关标签:
3条回答
  • 2020-12-31 02:49

    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>
    
    0 讨论(0)
  • 2020-12-31 02:54

    If you have more complicated layout and cannot bind to the ScrollViewer then the IgnoreWidthControl in the answer https://stackoverflow.com/a/41281041/254109 may help.

    0 讨论(0)
  • 2020-12-31 02:56

    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>
    
    0 讨论(0)
提交回复
热议问题