在渲染之前确定 SizeToContent WPF 窗口的大小

发布于 2024-08-19 15:30:00 字数 574 浏览 3 评论 0原文

我的 WPF 应用程序中有时会显示一个窗口。当它显示时,它会随着动画淡入,而当关闭时,它会淡出。没什么特别的,只是一个修改不透明度的故事板。实际上窗口从未真正关闭,不透明度只是淡出到 0,直到再次显示为止。

该窗口是一个信息窗口,并不总是显示相同的内容。它的大小适合内容(宽度和高度),并且在这方面效果很好。用户选择要显示的屏幕基本区域(左上、右上、中、左下、右下)。

在窗口淡出之前,内容已更新。由于窗口的大小会根据内容而增加或减小。宽度和高度可以改变。

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

逻辑是有效的,但我遇到的问题是窗口的高度和宽度没有返回内容更新后的大小,而是返回上次显示时的大小。我假设这是因为它尚未用新内容呈现。

这让我感到悲伤,因为如果窗口比上次更大,它显然会延伸到屏幕之外。

我尝试将窗口定位在 OnContentRendered 事件中,但这仅在创建窗口时触发一次,而不是在内容更新后、不透明度设置为 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.

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.

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

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?

Any help would be appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-08-26 15:30:01

我找到了我自己问题的答案。更新内容后,只需调用窗口的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文