How to keep the aspect ratio of a UserControl?

11,155

Solution 1

I'm not sure this will work, but if you register a handler for the SizeChanged event and in there put in your code keep the aspect ratio 1:1.

The SizeChangedEventArgs argument has the old size and the new size so you can check which has changed and update the other accordingly.

You might need to introduce a guard variable so that you don't get a cascade of SizeChanged events as a result of updating the Height or Width.

Solution 2

Another alternative:

<local:MyControl Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>

Solution 3

Try using a ViewBox and setting its Stretch property to Uniform

Share:
11,155
TastyEquation60
Author by

TastyEquation60

Head of Product at divio.com, the company behind Aldryn and backers of django-CMS.

Updated on June 14, 2022

Comments

  • TastyEquation60
    TastyEquation60 almost 2 years

    Does anyone have an idea how to keep the Height/Width Ratio 1:1 of a UserControl?

    E.g. if Height > Width, Width & Height will have the same size and vice versa.