如何在 WPF 中获取折叠控件的宽度/高度? [英] How to get the Width/Height of a collapsed control in WPF?

查看:29
本文介绍了如何在 WPF 中获取折叠控件的宽度/高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以在控件折叠时在运行时在 WPF 中获取控件的宽度.

im wondering if theres an easy way to get the width of a control in WPF at runtime while the control is collapsed.

当我使用 control.Width 时会得到以下结果:-1.#IND并且 control.actualWidth 将返回 0.0 因为它已折叠.

when i use control.Width ill get the following result: -1.#IND and control.actualWidth will return 0.0 because its collapsed.

我想调整窗口大小,然后显示折叠的控件.

i want to resize my window and then display the collapsed control.

谢谢


一些细节

我的窗口中有一个包含 2 列的网格,第一列包含一个选项卡控件,第二列包含一个扩展器控件.我想在扩展扩展器控件时扩展窗口的宽度,因此第一列中的内容将保持其大小.

推荐答案

您希望得到什么尺寸?

大小不仅取决于控件,还取决于其容器.因此,除非实际呈现控件,否则无法确定实际大小.

The size is not just dependent on the control but also on its container. So the actual size can not be determined unless the control is actually rendered.

您可以将其设置为 Invisible,而不是使用 Collapsed,这样它的大小将根据其自身的逻辑和容器的逻辑进行调整.

Instead of using Collapsed you could make it Invisible that way it will be sized by its own logic and the logic of the container.

编辑

在评论中很清楚需要控件大小的原因是什么:

In the comments it became clear that what the reason was for needing the size of the control:

我的网格中有 2 列窗口,第一列包含一个选项卡控件,第 2 列 a 包含一个扩展器控制.我想延长展开时窗口的宽度扩展器控制,所以内容在第一列将保持其大小.

I have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column a holds an expander control. i want to extend the width of my window when expanding the expander control, so the content in the 1st column will remain its size.

我的回答:

将窗口的 SizeToContent 设置为 WidthAndHeight 并将两个网格列的宽度设置为 auto.那应该照顾它.

Set the SizeToContent of the window to WidthAndHeight and set the width of both grid columns to auto. That should take care of it.

这篇关于如何在 WPF 中获取折叠控件的宽度/高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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