首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过在代码中设置高度和宽度来缩小视图的大小不会正确更新

通过在代码中设置高度和宽度来缩小视图的大小不会正确更新
EN

Stack Overflow用户
提问于 2019-03-26 05:02:42
回答 1查看 26关注 0票数 0

在我的应用程序中,我们有一个包含许多嵌套视图的视图,以便提供一些常见的功能,例如:

在本例中:

具有*大小的主内容网格单元格和固定宽度单元格的ErrorPanelView (UserControl),其中包含验证消息面板(如果任何验证失败)-此面板具有固定宽度,并根据需要可见或折叠。

主内容再次包含一个ButtonPanelView,其中包含一个*大小的内容网格单元格和一个固定高度的单元格,后者包含标准按钮(ok、cancel等)

然后,主内容包含“实际”视图-这可能有一个min/maxHeight & min/maxWidth,在本例中,min/max值是相同的,因此大小是静态的。

当视图出现验证错误时,validationPanel将变为可见。整个视图将按预期调整大小(扩展宽度),到目前为止一切正常。但是,当用户处理验证错误时,验证面板应该消失,并且窗口大小应该缩小到其原始大小。实际发生的情况是,内部视图将正确地调整大小(至少看起来是这样),但外部视图( ErrorPanelView用户控件)将保持较大的尺寸,并在以前验证面板可见的位置留下一个黑色字段。

当用户单击用户控件时,usercontrol将捕捉到其预期的(原始)大小。

我不知道我在这里漏掉了什么。

据我所知,尺寸计算正确,用户控件的高度和宽度被设置为正确的值,但是序列有什么问题吗?--或者我应该显式地调用一些systemEvent?

或者完全是别的什么??

我被难住了。

代码语言:javascript
运行
复制
private static void CalculateWindowSize(BaseWindow window, List<View> views, bool adjustCurrentSize)
        {
            // Calculate the extra space the window consumes.
            View topView = views.First();
            double heightDiff = window.ActualHeight - topView.ActualHeight;

            double widthDiff = window.ActualWidth - topView.ActualWidth;
            double ww = window.Width; //
            double tvw = topView.Width;


            const double zeroTolerance = 0.6;
            double maxHeightMax = 0, maxWidthMax = 0;
            double maxHeightSum = 0, maxWidthSum = 0;
            double minHeightMax = 0, minWidthMax = 0;
            double minHeightSum = 0, minWidthSum = 0;
            double heightSum = 0, widthSum = 0;
            double heightMax = 0, widthMax = 0;
            foreach (View view in views)
            {
                ViewSizeBehaviour viewHeightBehaviour = GetHeightSizeBehaviour(view);
                ViewSizeBehaviour viewWidthBehaviour = GetWidthSizeBehaviour(view);

                switch (viewHeightBehaviour)
                {
                    case ViewSizeBehaviour.Sum:
                    {
                        if (!double.IsInfinity(view.MaxHeight)) maxHeightSum += view.MaxHeight;
                        if (Math.Abs(view.MinHeight) > zeroTolerance) minHeightSum += view.MinHeight;

                        // Default to MinHeight if WindowHeight not defined
                        if (!double.IsNaN(view.WindowHeight)) heightSum += view.WindowHeight;
                        else heightSum += view.MinHeight;
                        break;
                    }
                    case ViewSizeBehaviour.Max:
                    {
                        if (!double.IsInfinity(view.MaxHeight)) maxHeightMax = Math.Max(maxHeightMax, view.MaxHeight);
                        if (Math.Abs(view.MinHeight) > zeroTolerance) minHeightMax = Math.Max(minHeightMax, view.MinHeight);

                        // Default to MinHeight if WindowHeight not defined
                        heightMax = Math.Max(heightMax, !double.IsNaN(view.WindowHeight) ? view.WindowHeight : view.MinHeight);
                        break;
                    }
                }

                switch (viewWidthBehaviour)
                {
                    case ViewSizeBehaviour.Sum:
                    {
                        if (!double.IsInfinity(view.MaxWidth)) maxWidthSum += view.MaxWidth;
                        if (Math.Abs(view.MinWidth) > zeroTolerance) minWidthSum += view.MinWidth;

                        // Default to MinWidth if WindowWidth not defined
                        if (!double.IsNaN(view.WindowWidth)) widthSum += view.WindowWidth;
                        else widthSum += view.MinWidth;
                        break;
                    }
                    case ViewSizeBehaviour.Max:
                    {
                        if (!double.IsInfinity(view.MaxWidth)) maxWidthMax = Math.Max(maxWidthMax, view.MaxHeight);
                        if (Math.Abs(view.MinWidth) > zeroTolerance) minWidthMax = Math.Max(minWidthMax, view.MinWidth);

                        // Default to MinWidth if WindowWidth not defined
                        widthMax = Math.Max(widthMax, !double.IsNaN(view.WindowWidth) ? view.WindowWidth : view.MinWidth);
                        break;
                    }
                }
            }

            if (adjustCurrentSize)
            {
                window.Height = Math.Max(heightSum, heightMax) + heightDiff;
                window.Width = Math.Max(widthSum, widthMax) + widthDiff;
            }

            // Only set maximum height & width if they are greater than zero. Otherwise all views have specified Infinity as max height

            double maxHeight = Math.Max(maxHeightSum, maxHeightMax);
            double maxWidth = Math.Max(maxWidthSum, maxWidthMax);
            if (Math.Abs(maxHeight) > zeroTolerance)
            {
                window.MaxHeight = maxHeight + heightDiff;
                if (window.MaxHeight < window.Height || window.MaxHeight < window.ActualHeight)
                {
                    window.Height = window.MaxHeight;
                }
            }
            if (Math.Abs(maxWidth) > zeroTolerance)
            {
                window.MaxWidth = maxWidth + widthDiff;
                if (window.MaxWidth < window.Width || window.MaxWidth < window.ActualWidth)
                {
                    window.Width = window.MaxWidth;
                }
            }

            // Minimum height & width are the maximum of either the sum or max calculations

            window.MinHeight = Math.Max(minHeightSum, minHeightMax) + heightDiff;
            window.MinWidth = Math.Max(minWidthSum, minWidthMax) + widthDiff;
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55346368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档