WPF FAQ - Canvas

How do I position the children of a Canvas at a discreet X but stretch them vertically to fill?

You can do so by binding the child’s Height to the Canvas’s ActualHeight, as follows:

[XAML]

<Canvas Name='myCanvas'>
	<Grid Canvas.Left='10' Width='20' Height='{Binding ElementName=myCanvas, Path=ActualHeight}' >
	</Grid>
</Canvas>
Permalink
How can I use the attached properties of Canvas to position child elements ?

This example shows how to use the attached properties of Canvas to position child elements.

[XAML]

<Page xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' WindowTitle='Canvas Attached Properties Sample'>
  <Border HorizontalAlignment='Left' VerticalAlignment='Top' BorderBrush='Black' BorderThickness='2'>
    <Canvas Background='LightBlue' Width='400' Height='400'>
      <Button Canvas.Top='50'>Canvas.Top='50'</Button>
      <Button Canvas.Bottom='50'>Canvas.Bottom='50'</Button>
      <Button Canvas.Left='50'>Canvas.Left='50'</Button>
      <Button Canvas.Right='50'>Canvas.Right='50'</Button>
    </Canvas>
  </Border>    
</Page>
Permalink