Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:31:12+00:00 2026-06-12T22:31:12+00:00

I have a UserControl and that UserControl has to be resized with aspect ratio.

  • 0

I have a UserControl and that UserControl has to be resized with aspect ratio.
That means: width:height = 2:1.
Currently I am using this code:

    protected override Size ArrangeOverride(Size arrangeBounds)
    {
        if (ActualWidth == 0 || ActualHeight == 0) return arrangeBounds;
        base.ArrangeOverride(arrangeBounds);
        double ratio = 2;

        if (Parent != null)
        {
            var size = new Size(arrangeBounds.Height * ratio, arrangeBounds.Height);

            double containerWidth = ((FrameworkElement)Parent).ActualWidth;
            if (containerWidth < size.Width)
            {
                double newHeight = arrangeBounds.Height * (containerWidth / size.Width);
                canvas.Width = newHeight * ratio;
                canvas.Height = newHeight;
            }
            else
            {
                canvas.Width = size.Height * ratio;
                canvas.Height = size.Height;
            }
        }

        return arrangeBounds;
    }

But it is not really working. That means it works but not every time. If I max. the window it sometimes does not get resized, so its a bit “random” if the control gets resized. So if someone would have a better solution if would be very nice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:31:15+00:00Added an answer on June 12, 2026 at 10:31 pm

    It’s a bit late, but I recently came across the same problem and since I did not find a good solution I decided to write my own layout control/decorator and wrote a blog post about it here:

    http://coding4life.wordpress.com/2012/10/15/wpf-resize-maintain-aspect-ratio/

    Basically my solution was to overwrite both MeasureOverride and ArrangeOverride. So far it works very nicely in all of the common containers and I didn’t encounter any problems like you described.

    I recommend reading the post, where you find a working decorator control, but the most important methods are these:

       protected override Size MeasureOverride(Size constraint)
       {
          if (Child != null)
          {
             constraint = SizeToRatio(constraint, false);
             Child.Measure(constraint);
    
             if(double.IsInfinity(constraint.Width)
                || double.IsInfinity(constraint.Height))
             {
                return SizeToRatio(Child.DesiredSize, true);
             }
    
             return constraint;
          }
    
          // we don't have a child, so we don't need any space
          return new Size(0, 0);
       }
    
       protected override Size ArrangeOverride(Size arrangeSize)
       {
          if (Child != null)
          {
             var newSize = SizeToRatio(arrangeSize, false);
    
             double widthDelta = arrangeSize.Width - newSize.Width;
             double heightDelta = arrangeSize.Height - newSize.Height;
    
             double top = 0;
             double left = 0;
    
             if (!double.IsNaN(widthDelta)
                && !double.IsInfinity(widthDelta))
             {
                left = widthDelta/2;
             }
    
             if (!double.IsNaN(heightDelta)
                && !double.IsInfinity(heightDelta))
             {
                top = heightDelta/2;
             }
    
             var finalRect = new Rect(new Point(left, top), newSize);
             Child.Arrange(finalRect);
          }
    
          return arrangeSize;
       }
    
       public Size SizeToRatio(Size size, bool expand)
       {
          double ratio = AspectRatio;
    
          double height = size.Width / ratio;
          double width = size.Height * ratio;
    
          if (expand)
          {
             width = Math.Max(width, size.Width);
             height = Math.Max(height, size.Height);
          }
          else
          {
             width = Math.Min(width, size.Width);
             height = Math.Min(height, size.Height);
          }
    
          return new Size(width, height);
       }
    

    I hope it helps someone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UserControl that has a repeating-image background set with the following code:
i have an usercontrol that has a public event. when i use this user
I have a UserControl that has a BaseClass object as a public member. Right
I have created a UserControl that has 3 panels. What I what to do
Suppose I have a UserControl whose DataContext is set to an object that has
I have a UserControl that is part of a Class library. I reference this
In other words, I have a UserControl that has, for example, a label on
I have an ASP.Net application that has a UserControl that needs to be rendered
I have a usercontrol that has a popup that displays another usercontrol when the
I have created a UserControl that has a ListView in it. The ListView is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.