Stretch items to fill canvas

前端 未结 2 1136
一整个雨季
一整个雨季 2021-01-01 15:21

I have a Dockpanel with items inside a Canvas. The Dockpanel and any other items (Grid etc) that I place inside the Canvas, only take up their minimum required space. How do

相关标签:
2条回答
  • 2021-01-01 15:45

    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}">
    
    0 讨论(0)
  • 2021-01-01 15:49

    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.

    0 讨论(0)
提交回复
热议问题