简体   繁体   English

触发父(画布)Size_Changed时,不会触发Line Size_Changed事件

[英]Line Size_Changed event doesn't fire when parent (canvas) Size_Changed is fired

I have a canvas on which I want to draw several lines. 我有一个要在上面画几条线的画布。 Which works fine. 哪个工作正常。 But I also want to move the lines, when the size of the canvas changes to a new position. 但是,当画布的尺寸更改为新位置时,我也想移动线条。 So I've added an eventhandler of Size_Changed to each line 所以我在每一行中添加了一个Size_Changed事件处理程序

liMonth.SizeChanged += LiMonth_SizeChanged;

However, when I change the size of the parent window, the Size_Changed Event of the Canvas is fired, but not the Size_Changed event of the line. 但是,当我更改父窗口的大小时,将触发Canvas的Size_Changed事件,但不会触发该行的Size_Changed事件。

How do I get the canvas to fire the size_changed event for its children, when the size changes? 当尺寸更改时,如何让画布为其子级触发size_changed事件?

I have the same problem with a rectangle, which is also on the canvas, but I figure it is the same issue. 我在画布上也遇到一个矩形的相同问题,但我认为这是相同的问题。

A Canvas never resizes its child elements, hence there is no SizeChanged event fired for your Lines and Rectangles. Canvas从不调整其子元素的大小,因此不会为您的线和矩形触发SizeChanged事件。

Put the repositioning code in a handler for the Canvas' SizeChanged event: 将重新定位代码放入Canvas的SizeChanged事件的处理程序中:

private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Canvas canvas = (Canvas)sender;

    foreach (var line in canvas.Children.OfType<Line>())
    {
        line.X1 = ...
        line.X2 = ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM