简体   繁体   English

WPF控件上的SizeChanged事件Event-args没有返回正确的Height值

[英]SizeChanged event on WPF control Event-args don't return proper Height value

I developing an application in which I need to integrate scaling of the UI.Althrough it's something trivial to do, I experience some problems. 在开发一个需要集成UI缩放比例的应用程序的过程中,这是一件微不足道的事情,但我遇到了一些问题。 My UI isn't particulary clean and well formated to begin with(the XAML). 我的UI并不是特别干净,并且格式正确(以XAML开头)。

I have a Grid which looks like this and acts as a wrapper for all other controls. 我有一个看起来像这样的网格,并充当所有其他控件的包装。 It has the same height and width as the height & width of the window 它的高度和宽度与窗口的高度和宽度相同

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

Then I got a lot of other nested Grid's inside this grid and some other controls.The number of all controls is ~200. 然后我在这个网格和其他一些控件中有很多其他嵌套的Grid,所有控件的数量约为200个。

Then I register the SizeChanged event for the "parentBox" named control which I noted above, and I have the following recursive algorithm to scale the controls. 然后,我为上面提到的名为“ parentBox”的控件注册了SizeChanged事件,并且具有以下递归算法来缩放控件。

Registered Event ----- 注册活动-----

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);
    }
}

Function to scale all children. 缩放所有孩子的功能。

    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;
                            }
                        }
                    }
                }
            }
        }
    }

While I debug and I scale the height of the Window the "parentBox" control the SizeChanged event fires only when I scale the window from the width side and nothing occurs when I try to do change the height.I'm suspecting it's something to do with my control placements which are inside the nested inside "parentBox" grids. 在调试和缩放窗口的高度时,“ parentBox”控件的SizeChanged事件仅在我从宽度方向缩放窗口时才触发,并且在尝试更改高度时什么也没有发生。我怀疑这是有必要的我的控件放置在嵌套在“ parentBox”网格内的控件中。

I appreciate your time to read this question.Thanks. 感谢您抽出宝贵时间阅读此问题。谢谢。

You have defined the fixed size for Grid(Height="511"). 您已经为Grid(Height =“ 511”)定义了固定大小。

Size changed event won't be called when you set the fixed size. 设置固定尺寸时,不会调用尺寸更改事件。 You didn't change the width. 您没有更改宽度。 That's why event is triggered when you change the width. 因此,更改宽度时会触发事件。

You need to remove the Height. 您需要删除高度。 By default height and width will be applied from the parent. 默认情况下,高度和宽度将从父级应用。

Regards, 问候,
Dhanasekar Dhanasekar

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

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