13

I have a Viewbox with Stretch=Uniform in order to not distort the content. However, when the frame window is wider or taller than the content, the Viewbox content is always centered.

I cannot seem to find any content alignment options on the Viewbox. Is there a way to do this?

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Jeff B
  • 1,856
  • 2
  • 17
  • 28

2 Answers2

21

Try VerticalAlignment="Top" and HorizontalAlignment="Left" on your viewbox. It will cause it to be anchored to the top and left side.

<Grid>
    <Viewbox VerticalAlignment="Top" HorizontalAlignment="Left">
    ...
    </Viewbox>
</Grid>

If you want it to completely fill (but keep it uniform) you can use Stretch="UniformToFill"

boop
  • 7,413
  • 13
  • 50
  • 94
John Z
  • 431
  • 2
  • 3
1

According to MSDN the Viewbox is used to stretch the child elements. Since the child elements would be stretched, you would have to set the content alignment of the children.

You may want to look at this for more information on the Viewbox: How do I keep aspect ratio on scalable, scrollable content in WPF?

Community
  • 1
  • 1
a_hardin
  • 4,991
  • 4
  • 32
  • 40
  • 2
    I can set HorizontalAlignment and VerticalAlignment on the child object and it still centers when the Viewbox scales it. – Jeff B Dec 04 '08 at 21:31
  • 1
    What I see is that you want to set alignment on the Viewbox _itself_ (as in John Z's answer). Strangely enough this does cause the contents to align similarly. That's an odd thing, since it naturally also aligns the ViewBox in _its_ container. – Josh Sutterfield Mar 15 '12 at 19:52