在渲染前确定SizeToContent WPF窗口的大小 [英] Determine size of SizeToContent WPF Window before its rendered

查看:351
本文介绍了在渲染前确定SizeToContent WPF窗口的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序中有一个窗口,有时会显示一个窗口.当它显示时,它会随着动画淡入,而当它关闭时,它会淡出.没什么好看的,只是修改不透明度的情节提要.实际上,窗口从未真正关闭过,不透明度会逐渐淡出为0,直到不显示为止.

I have a window in my WPF application that is displayed on occasion. When it is shown it is faded in with an annimation, and when closed it is faded out. Nothing fancy, just a storyboard that modifies the opacity. Actually the window is never really closed, the opacity is just faded out to 0 where it remains until its to be displayed again.

此窗口是一个信息窗口,并不总是显示相同的内容.它的大小适合内容(宽度和高度),并且在这方面效果很好.用户选择要显示的屏幕的基本区域(TopLeft,TopRight,Center,BottomLeft,BottomRight).

This window is an informative window and doesn't always show the same content. It is sized to content (Width and Height) and works well in that regard. The user chooses the basic area of the screen for it to be displayed (TopLeft, TopRight, Center, BottomLeft, BottomRight).

在窗口淡入之前,内容将被更新.因为窗口的大小适合内容,所以它的大小会增加或减小.宽度和高度可以改变.

Before the window is faded in the content is updated. Because the window is sized to content it increases or descreases in size. The width and height can change.

定位窗口时,可以说,在右下角,我只是获取屏幕的工作区域(宽度和高度),然后减去窗口的宽度/高度,即可获得所需的顶部和左侧位置.

When positioning the window, lets say, in the bottom right corner, I simply take the WorkingArea of the screen (width and height) and then minus the width/height of the window to get the Top and Left position that I need.

逻辑工作正常,但是我遇到的麻烦是窗口的高度和宽度没有返回内容更新后的大小,而是返回了上次显示时的大小.我认为这是因为尚未使用新内容进行渲染.

The logic works, but the trouble I am having is the Window's Height and Width is not returning the size it is after the content was updated, but is returning the size it was the last time it was displayed. I am assuming this is because it hasn't yet been rendered with the new content.

如果Window大于它最后一次明显超出屏幕的范围,这会使我感到悲伤.

This causes me grief becuase if the Window is larger than it was the last time it obviously extends off the screen.

我尝试将窗口放置在OnContentRendered事件中,但这仅在创建Window时触发一次,而不是在不透明度设置为0时在内容更新之后触发.

I tried positioning the window in the OnContentRendered event, but this only fires once when the Window is created, not after the content has been updated, when the opacity is set to 0.

有人知道我在淡入之前如何获得该窗口的准确宽度和高度吗?

Does anyone have any idea how I might get an accurate width and height of this window before it is faded in?

任何帮助将不胜感激!

推荐答案

我找到了自己问题的答案.更新内容后,只需简单地调用窗口的UpdateLayout()方法即可强制进行更新.调用宽度和高度,然后返回准确的值.

I found the answer to my own question. After updating the content a simple call to the window's UpdateLayout() method forces an update. Calls to the width and height then return accurate values.

这篇关于在渲染前确定SizeToContent WPF窗口的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆