-1

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?

GANI
  • 2,013
  • 4
  • 35
  • 69
  • "have to implement MeasureOverride and ArrangeOverride". That would be true for a derived Panel, but not for a Canvas. Just recalculate and set Left and Top of the child elements. Or use some other, more appropriate layout panel, like a Grid or DockPanel. – Clemens Aug 22 '17 at 06:36
  • am using a custom canvas derived from Canvas. – GANI Aug 22 '17 at 18:57
  • for downvote , please add comment – GANI Aug 23 '17 at 05:56

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • how to calculate the new left and top , based on new canvas size? all the controls are added dynamically. – GANI Aug 22 '17 at 14:27
  • How should I know? it is *your* logic. – mm8 Aug 22 '17 at 14:29
  • its not my logic, based on the question, when the canvas size is increased, how to find the new left based on new canvas width? A control is placed at left 50, when canvas width is (500), when canvas width increases want to set the left as per new width (left value should increase) – GANI Aug 22 '17 at 14:41
  • "As per new width..."? What's your forumula for calculating the new left value? – mm8 Aug 22 '17 at 14:43
  • you didn't got my question correct? if a control is placed in center of the canvas, the control should be in center, even when we view in bigger size window – GANI Aug 22 '17 at 14:48
  • Since when is 50 the center of 500...? – mm8 Aug 22 '17 at 14:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152560/discussion-between-gani-and-mm8). – GANI Aug 22 '17 at 18:57