Search code examples
wpfsilverlightlayoutscrollviewbox

Scrollable content inside WPF Viewbox with "UniformToFill"


How could I make the content which is put inside WPF Viewbox with Stretch="UniformToFill" be scrollable?

For example:

<Grid Height="500" Width="1000" >
  <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" >
      <Viewbox Stretch="UniformToFill">
       ......

      </Viewbox>
   </ScrollViewer>
</Grid>

The content is resized to fill the destination dimensions while it preserves its native aspect ratio. If the aspect ratio of the destination differs from the source, the source content is clipped to fit in the destination dimensions.
So I tried to use ScrollViewer to be able to scroll to the areas of the source content which were clipped, but the Scrollbars are visible but disabled.

I tried ClipToBounds="False" but it didn't help.


Solution

  • The ViewBox resizes its content based on the dimensions it occupies. The ScrollViewer gives its content infinite width/height to render. So, when you put a ViewBox inside a ScrollViewer, the ViewBox thinks it has "all the space in the world" to stretch.

    Also, the ViewBox uses render transformations to stretch the content, which means your ScrollViewer will never know the final size of the content.

    To make your ScrollViewer work, you have to put a Width/Height on the ViewBox. It needs to know how much space it's occupying.