简体   繁体   English

如何在WPF中获取折叠控件的宽度/高度?

[英]How to get the Width/Height of a collapsed control in WPF?

im wondering if theres an easy way to get the width of a control in WPF at runtime while the control is collapsed. 我想知道在控件崩溃时是否有一种简单的方法可以在运行时获取WPF中控件的宽度。

when i use control.Width ill get the following result: -1.#IND and control.actualWidth will return 0.0 because its collapsed. 当我使用control.Width ill得到以下结果:-1。#IND和control.actualWidth将返回0.0,因为它已折叠。

i want to resize my window and then display the collapsed control. 我想调整窗口大小,然后显示折叠控件。

thanks 谢谢

Edit: 编辑:
Some details 一些细节

i have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column holds an expander control. 我的窗口中有一个包含2列的网格,第1列包含选项卡控件,第2列包含扩展控件。 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. 我想在扩展扩展器控件时扩展窗口的宽度,因此第1列中的内容将保持其大小。

Put the control in question inside a container (like a ContentControl ) and collapse the container rather than the control itself. 将控件置于容器(如ContentControl )中并折叠容器而不是控件本身。 Then you should be able to simply call Measure (or use the DesiredSize property) on the control to determine how much room it wants. 然后你应该能够简单地在控件上调用Measure (或使用DesiredSize属性)来确定它想要多少空间。

You have to call the UpdateLayout method on the control or conainer of control. 您必须在控件或控件的conainer上调用UpdateLayout方法。 After that the things may work properly. 在那之后,事情可能正常。

In UWP you can determine size of collapsed control by making it visible for a sec and then hiding it again, change is not noticeable: 在UWP中,您可以通过使其在一秒内可见并再次隐藏它来确定折叠控件的大小,更改不明显:

var oldVisibility = myBorder.Visibility;
myBorder.Visibility = Visibility.Visible;
myBorder.UpdateLayout();
var height = myBorder.RenderSize.Height;
myBorder.Visibility = oldVisibility;

Post which is marked as an answer actually does not answer the question, it just gives a workaround. 标记为答案的帖子实际上没有回答问题,它只是提供了一种解决方法。

To get the size of the collapsed control you need: 要获得折叠控件的大小,您需要:

  1. Set control's visibility as Hidden (Collapsed won't evaluate). 将控件的可见性设置为隐藏(折叠不会评估)。
  2. Call Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)) method of the control. 呼叫测量(新大小(Double.PositiveInfinity,Double.PositiveInfinity))控制方法。
  3. Get the size from DesiredSize property of the control. 从控件的DesiredSize属性中获取大小。
  4. Then you can Collapse your control back. 然后你可以折叠你的控制。

What size do you expect to get? 你期望得到什么尺寸?

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. 因此,除非实际呈现控件,否则无法确定实际大小。

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. 而不是使用Collapsed,你可以通过它自己的逻辑和容器的逻辑来使它变得不可见。

EDIT 编辑

In the comments it became clear that what the reason was for needing the size of the control: 在评论中,很清楚,需要控件大小的原因是什么:

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. 我的窗口中有一个包含2列的网格,第1列包含选项卡控件,第2列包含扩展控件。 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. 我想在扩展扩展器控件时扩展窗口的宽度,因此第1列中的内容将保持其大小。

My answer: 我的答案:

Set the SizeToContent of the window to WidthAndHeight and set the width of both grid columns to auto. 将窗口的SizeToContent设置为WidthAndHeight,并将两个网格列的宽度设置为auto。 That should take care of it. 那应该照顾它。

I believe you're going about this the wrong way. 我相信你会以错误的方式解决这个问题。 You can set the Window Width and height to "Auto" and then it will take care of all the resizing stuff. 您可以将窗口宽度和高度设置为"Auto" ,然后它将处理所有调整大小的内容。

The problem arises whenever you directly set the Width property of any control(trust me I've done it). 只要你直接设置任何控件的Width属性就会出现问题(相信我,我已经完成了)。 Once you do that, you've told WPF hands off of resizing logic, I know what I'm doing. 一旦你这样做,你已经告诉WPF 手动调整逻辑,我知道我在做什么。

If you think something isn't resizing at the right time you can add a handler to some event and then call control.InvalidateVisual() or control.InvalidateMeasurement() which will make it go through a whole new layout pass. 如果你认为某些东西没有在正确的时间调整大小,你可以为某个事件添加一个处理程序,然后调用control.InvalidateVisual()control.InvalidateMeasurement() ,这将使它通过一个全新的布局传递。

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

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