WPF控件上的SizeChanged事件事件-参数没有返回正确的高度值

本文关键字:事件 返回 高度 参数 控件 SizeChanged WPF | 更新日期: 2023-09-27 18:13:14

我正在开发一个应用程序,我需要在其中集成UI的缩放。虽然这是一件微不足道的事情,但我遇到了一些问题。我的UI从一开始就不是特别干净和格式良好(XAML)。

我有一个Grid,它看起来像这样,并作为所有其他控件的包装器。它的高度和宽度与height &窗口宽度

<Grid x:Name="parentBox" Margin="10,10,2,0" Height="511" VerticalAlignment="Top">

然后我在这个网格和其他一些控件中得到了许多其他嵌套的网格。所有控件的数量为~200。

然后我为上面提到的名为"parentBox"的控件注册sizechange事件,并且我有以下递归算法来缩放控件。

已注册事件-----

private void ParentSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
    if (e.PreviousSize.Width != 0 && e.PreviousSize.Height != 0)
    {
        double coefficientHeight = e.NewSize.Height / e.PreviousSize.Height;
        double coefficientWidth = e.NewSize.Width / e.PreviousSize.Width;
        Scale(Parent, coefficientWidth, coefficientHeight);
    }
}

缩放所有子函数。

    private void Scale(Grid Parent, double coefficientW, double coefficientH)
    {
        if (Parent.Children.Count == 0)
        {
            return;
        }
        else
        {
            if (coefficientH > 0 && coefficientW > 0)
            {
                Parent.Width *= coefficientW;
                Parent.Height *= coefficientH;
                for (int y = 0; y < Parent.Children.Count; y++)
                {
                    var child = Parent.Children[y];
                    if (child is Grid)
                    {
                        var childG = child as Grid;
                        Scale(childG, coefficientW, coefficientH);
                    }
                    else
                    {
                        if (child is ElementRepresentation)
                        {
                            var element = child as ElementRepresentation;
                            element.Width *= coefficientW;
                            element.Height *= coefficientH;
                            if (element.Height < 25 || element.Width < 25)
                            {
                                if (!ElementNames.ContainsKey(int.Parse(element.elementNumber.Text)))
                                {
                                    ElementNames.Add(int.Parse(element.elementNumber.Text), element.ElementLongName.Text);
                                    element.ElementLongName.Text = String.Empty;
                                }
                            }
                            if (element.ElementLongName.Text == String.Empty) 
                            {
                                element.ElementLongName.Text = ElementNames[int.Parse(element.elementNumber.Text)];
                            }
                        }
                        else
                        {
                            if(child is Label)
                            {
                                var label = child as Label;
                                label.Width *= coefficientW;
                                label.Height *= coefficientH;
                            }
                        }
                    }
                }
            }
        }
    }

当我调试并缩放窗口的高度时,"parentBox"控件的SizeChanged事件仅在我从宽度方面缩放窗口时触发,当我试图改变高度时没有发生任何事情。我怀疑这与我的控件放置在嵌套的"parentBox"网格内有关。

感谢您花时间阅读这个问题。

您已经定义了Grid(Height="511")的固定大小。

当你设置固定大小时,

Size changed事件不会被调用。你没有改变宽度。这就是为什么当你改变宽度时事件会被触发。

您需要删除高度。默认情况下,高度和宽度将从父元素应用。


问候,Dhanasekar

WPF控件上的SizeChanged事件事件-参数没有返回正确的高度值