locked
Resize Elements inside the canvas RRS feed

  • Question

  • Canvas contains some UIelements like rectange, ellipse, button etc. I would like to resize child elements, when the canvas is resized. I'm able to resize the Canvas using Adorner. Can someone suggest me the general approach to resize any element that located inside the canvas. for e.g <Canvas><Button>Test Button</Button></Canvas>. In this case Button element should be resized with respect to the canvas. Thanks in advance, Venu
    Friday, September 29, 2006 6:38 PM

Answers

  • try this

    <Window x:Class="WindowsApplication2.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="WindowsApplication2" Height="300" Width="300"

    >

    <Viewbox Stretch="Fill">

    <Canvas Width="300" Height="300">

    <Button>dsoihsdfgdshi</Button>

    </Canvas>

    </Viewbox>

    </Window>

    Sunday, October 1, 2006 5:29 PM
  • I think use of viewbos will be the best practice,

     

    you should use adorner to resize the viewbox only, every child element, will resize on its own.

     

    Sunday, October 1, 2006 7:13 AM

All replies

  • Canvas was designed to not support automatic layout updates to its children.  It provides absolute positioning and sizing of the elements it contains.  Is there a reason why you need absolute positioning but relative sizing?
    Friday, September 29, 2006 7:32 PM
  • Instead of resizing the canvas, apply a scaletransform to it. That will propagate down to its children as well.
    Friday, September 29, 2006 10:00 PM
  • <viewbox><canvas> <uielements /> </canvas></viewbox>

    I don't know if you have to set the height/width on the viewbox/canvas or not, just try.

    Friday, September 29, 2006 10:28 PM
  • Thanks for your time. Reason: I have few UIElements (like rectangle, ellipse, button) that are grouped together. these are added to the canvas and considered as a single object. Resizing the canvas should resize its child UIelements.I tried "Mike" and "Chimme" suggestions, but couldn't solve my problem.a) ScaleTransform was not changing the width/height of the Canvas child elements.b) when canvas width/height changes, no effect on canvas child elements.Main objective is to resize the child elements with respect to its parent. would you suggest best approach in WPF?
    Saturday, September 30, 2006 4:50 PM
  • I think use of viewbos will be the best practice,

     

    you should use adorner to resize the viewbox only, every child element, will resize on its own.

     

    Sunday, October 1, 2006 7:13 AM
  • try this

    <Window x:Class="WindowsApplication2.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="WindowsApplication2" Height="300" Width="300"

    >

    <Viewbox Stretch="Fill">

    <Canvas Width="300" Height="300">

    <Button>dsoihsdfgdshi</Button>

    </Canvas>

    </Viewbox>

    </Window>

    Sunday, October 1, 2006 5:29 PM
  • Thanks very much.
    Wednesday, October 11, 2006 7:16 PM
  • Chimme, U R THE MAN. It took a while to find this i knew there was an easier way than window.resize thx very much for ur post, saved a lot a time for a newbie Smile
    Monday, November 12, 2007 7:11 PM
  • Thanks alot.. I was working with image applications and this blog resolved importatnt issues.. Cheers.
    Thursday, August 5, 2010 5:58 PM
  • Hello all,

     

    I am strucked with same requirement and in my case i cant use Viewbox & even scaleTransform becasue My user should able to check or uncheck resizing ...

     

    Can any help me in providing other solution for this ??

     

    Manoj.


    Thanks, Manoj
    Monday, March 7, 2011 12:35 PM
  • Thanks a lot man :D

    It worked!! Saved me a lot of time:)

    Monday, March 26, 2012 3:50 AM