21

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>
2
  • Clarification: Is the scrollviewer built into the control's template? Or is it external to the control?
    – Aviad P.
    Dec 30, 2009 at 16:48
  • You can pretend it looks like the above. Dec 30, 2009 at 17:19

2 Answers 2

28

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>
5
  • 1
    Yes, that's one way, although it can cause the layout engine to loop several times on the visual tree because the width binding happens after the rendering, which forces another layout pass.
    – Aviad P.
    Dec 30, 2009 at 20:34
  • Yes the proper way would be to modify the measure and arrange logic for either the scroll viewer, the textblock, or some custom element in between. I've been wracking my brain trying to figure it out since I wrote that comment 'I'm working on it' :)
    – Aviad P.
    Dec 30, 2009 at 22:16
  • 2
    I certainly notice it seems a little laggy on my machine (in my full app with complicated layout) when resizing the window, if you figure out a way to prevent the extra layout pass--that would be great. Dec 31, 2009 at 14:13
  • +1 - Thanks for taking the time to research this. This might be as good as it gets without a massive code rewrite :)
    – Aviad P.
    Jan 6, 2010 at 21:53
  • 6
    THANK YOU! THANK YOU! THANK YOU! OMG I just spent 2 hours looking for the solution and Binding to ViewportWidth did the trick! Thank you ever so much. Jun 2, 2010 at 7:42
2

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>
7
  • Hmm, this works in XamlPad, but not in my application. I will have to do some digging to figure out why. Dec 30, 2009 at 17:12
  • What exactly is the behavior you are trying to achieve? It should work when you set MinWidth and MinHeight on the TextBlock as well.
    – Aviad P.
    Dec 30, 2009 at 17:19
  • The TextBlock never resizes when you resize the window if you try the above in XamlPad. Dec 30, 2009 at 17:21
  • Oh I see what you mean, it's because of text wrapping. Working on it :)
    – Aviad P.
    Dec 30, 2009 at 17:31
  • My example below does not work 100%. When a vertical scrollbar appears, there is always a horizontal scrollbar visible even when there shouldn't be. Did you ever find another way? Otherwise I'll have to also bind to the ComputedScrollBarVisibility and subtract the SystemParameters.ScrollBarWidth (or whatever it's called). Jan 6, 2010 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.