silverlight tip: how to resize all objects in a canvas when the canvas resizes

subheading 3

Using a ScaleTransform on the canvas will scale all of it’s children by the same proportion!

18 responses to “silverlight tip: how to resize all objects in a canvas when the canvas resizes

  1. HshbvB Blogs rating, add your blog to be rated for free:

  2. Yeah, it gets a bit tricky doing this dynamically though. For example, I have a user control that updates the canvas scale when the user control is resized:

    public partial class ScalableControl : UserControl
    private bool constrainProportions;
    private double originalWidth;
    private double originalHeight;

    public ScalableControl()
    constrainProportions = true;
    originalWidth = 500;
    originalHeight = 200;

    private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
    double newScaleX = e.NewSize.Width / originalWidth;
    double newScaleY = e.NewSize.Height / originalHeight;
    if (constrainProportions)
    CanvasScale.ScaleX = CanvasScale.ScaleY =
    (newScaleX < newScaleY) ? newScaleX : newScaleY;
    CanvasScale.ScaleX = newScaleX;
    CanvasScale.ScaleY = newScaleY;

  3. DangerousDave

    Dear Jed Hunsaker,
    Can you update the inline code for this resize code? When are you calling the resizing method?
    Give few more details.

  4. Yeah, so you just create a new UserControl. In the example above, I’ve named it “ScalableControl”. In the XAML, in the tag, I’ve specified SizeChanged=”UserControl_SizeChanged”. That’s where I’m handling it.

    The only change I would make to “update” the above code is to create a loaded event to write the originalWidth and originalHeight instead. That way, it’s more dynamic.

  5. You know, I’ve been having major issues with this, even after what I’ve posted. Depending on how it’s used, you can get some really crazy fly-off-the-screen effects in Blend that make resizable controls totally bogus!

    I can’t figure out a perfect solution.

  6. welcome to the wonderful world of css err xaml. hours and hours and f’ing hours spent trying to find a way to bounce this off that as a percentage from whatever without killing your layout so people who can’t program won’t be left out in the cold by the system.

    it shouldn’t be this hard to tell wpf to treat an object’s placement and size as a percentage of its parent.

  7. Great write-up, I am regular visitor of one¡¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time.

  8. Thanks a bunch for sharing this with all folks you actually know what you are speaking about! Bookmarked. Kindly also talk over with my site =). We may have a link alternate arrangement among us

  9. Pingback: Tapety na Androida

  10. Not to hijack this thread, but, I want to track down an agent/advisor that was my parents’ fiduciary. I’m totally stuck here because I want to work with the same agent but, i can’t find them in any of the local directories online. Carlton Financial Group – 106 Mission Ct #701 Franklin, TN 37067 phone (615) 794-2536 Can someone recommend a way that is free to search for them?

  11. Great to know the — in depth from this blog.This will really help for my forward steps to be taken.

  12. Undeniably believe that which you stated. Your favorite reason appeared to
    be on the web the simplest thing to be aware of. I say to you, I certainly get annoyed
    while people consider worries that they plainly do not know about.
    You managed to hit the nail upon the top and defined out the whole thing without having side effect , people can take a signal.
    Will probably be back to get more. Thanks

  13. Hi, i think that i saw you visited my blog thus i came to “return the favor”.I am trying to find
    things to improve my web site!I suppose its ok to
    use some of your ideas!!

  14. I got this web site from my pal who shared with me concerning
    this website and now this time I am visiting this site and reading
    very informative articles here.

  15. Usap air mata anda dan katakan kepada diri anda sendiri bahwa hidup harus tetap berjalan

  16. Makanan enak yang ditawarkan ke pria yang sedang marah akan memedam amarahnya

  17. Tak peduli seburuk apapun masa lalumu cintai dirimu Hari ini kamu bisa memulai yg baru Beri yg terbaik tuk masa depanmu

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s