GANI
GANI

Reputation: 2049

How to rearrange children of canvas on ArrangeOverride wpf

I have canvas, where children are add dynamically to the canvas, lets say an image is placed at left = 50, top 50 when canvas width (500) and height(200). When the window is maximized the canvas width and height changes ( 1000, 400), this time want to rearrange the image position as per canvas width and height. Based on research found that, have to implement

MeasureOverride and ArrangeOverride methods of panel.

Here user may add any number of items to the canvass, want to scale the children relative to the canvas size.

How to implement the logic in above methods?

Upvotes: -1

Views: 686

Answers (1)

mm8
mm8

Reputation: 169200

You could your implementing your logic (whatever that is) in an event handler for the SizeChanged event:

private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Canvas canvas = sender as Canvas;
    Size size = e.NewSize;
    foreach (UIElement child in canvas.Children)
    {
        //set the new postion of each element according to your logic
        Canvas.SetLeft(child, );
    }
}

<Canvas x:Name="canvas" Width="500" Height="200" Background="Yellow" SizeChanged="Canvas_SizeChanged">
    <Button Content="..." Canvas.Left="50" Canvas.Top="50" />
</Canvas>

There is no reason to create a custom Panel and override the MeasureOverride and ArrangeOverride methods.

Upvotes: 1

Related Questions