1

How can canvas in wpf be autosized? I have a canvas in scrollviewer and I will add several buttons and lines in this canvas in code behind. since I don't know the position of the buttons, I have to hard code a very large number for the width and height of the canvas or if I add too many buttons, it can only show part of them.

I try to set the Width and Height to Auto but it doesn't work.

  <Grid>
      <ScrollViewer HorizontalScrollBarVisibility="Visible"    VerticalScrollBarVisibility="Visible">
          <Canvas Width="Auto" Height="Auto" Name="cv1"></Canvas>
      </ScrollViewer>
  </Grid>
josiah
  • 11
  • 2

1 Answers1

1

The Canvas element is the only element that can not be automatically resized, because has no inherent layout characteristics. If you want the Control to be resized as child elements come in, you could use something deriving from Grid. Try a UniformGrid instead of your Canvas ans fill it with the elements you want. It allows you to just add elements without any layout constraints that are handled by the UniformGrid. otherwise if you use a simple Grid, you will have to define a Position for your element by setting the Margin property of each child element.

Hope this helps.

jhontarrede
  • 124
  • 4