Stretch items to fill canvas Stretch items to fill canvas wpf wpf

Stretch items to fill canvas


The Canvas panel doesn't really support that.

It's very basic - it just allows you to position children absolutely by using Top, Bottom, Left and Right, and it always gives them just the space they need.

So usually you would use a Grid with just 1 column and 1 row instead.

You can however bind the width and height of the DockPanel to the width and height of the Canvas. That way the DockPanel will always fill the Canvas.

<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch"             Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0"            Width="{Binding ActualWidth, ElementName=InfoCanvas}"            Height="{Binding ActualHeight, ElementName=InfoCanvas}">


What you can do is:

<Grid>    <Canvas x:Name="InfoCanvas">        <!--Elements with canvas layout here-->    </Canvas>    <DockPanel x:Name="ReferenceInfo">        <!--Elements with dockpanel layout here-->    </DockPanel></Grid>

By wrapping both panels in a grid like this you can place elements that you cant to position relative to left, top etc in the canvas. Both the canvas and the dockpanel will fill available space. Note that the elements in the dockpanel will be rendered above the elements in the canvas when the dockpanel is defined after in xaml.

I'm assuming the code you posted is pseudo code, if not you should just remove the canvas.