WPF 中的宽度和实际宽度有什么区别?
我目前正在 WPF 中使用 Panel
,我注意到关于 Width
和 Height
属性,还有另外两个属性,称为实际宽度
和实际高度
。
实际宽度
获取此元素的渲染宽度 元素。 这是一个依赖 财产。 (继承自 框架元素。)
宽度
获取或设置元素的宽度。 这是一个依赖属性。 (继承自FrameworkElement。)
参考:MSDN< /a>
谁能指出两者之间的差异以及何时使用其中之一?
I am currently working with Panel
s in WPF, and I noticed that as regards the Width
and Height
properties, there are also two other properties called ActualWidth
and ActualHeight
.
ActualWidth
Gets the rendered width of this
element. This is a dependency
property. (Inherited from
FrameworkElement.)
Width
Gets or sets the width of the element.
This is a dependency property.
(Inherited from FrameworkElement.)
Reference: MSDN
Can anyone point out the differences between the two and when to use either one ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Width
/Height
是请求的或布局的尺寸。 如果您设置为 Auto,则当您在代码隐藏中访问该属性时,该值为double.NaN
。ActualWidth
/ActualHeight
和RenderSize.Width
/RenderSize.Height
均返回元素的渲染大小,如 RenderSize 的类型为Size。 如果您想要/需要项目的实际大小,请使用这些属性中的任何一个。Width
/Height
is the requested or layout size. If you set to Auto, then the value isdouble.NaN
when you access the property in code behind.ActualWidth
/ActualHeight
andRenderSize.Width
/RenderSize.Height
both return the element's rendered size, as RenderSize is of type Size. If you want/need the actual size of the item, then use any of these attributes.当我想将一个元素的宽度或高度绑定到另一个元素时,我发现 ActualWidth 最有用。
在这个简单的示例中,我有两个并排排列的按钮,下面的注释受限于包含这两个按钮的 StackPanel 的宽度。
I find
ActualWidth
most useful when I want to bind the width or height of one element to another.In this simple example I have two buttons arranged side by side and a comment underneath that is constrained to the width of the StackPanel containing the two buttons.
ActualWidth
考虑了值中的填充,因此任何时候您需要知道该数字时,都可以调用Actualwidth
而不是宽度并避免计算。编辑:删除了边距,因为它不是实际宽度的一部分。
ActualWidth
accounts for padding in the value so anytime you need to know that number you can callActualwidth
instead of width and avoid the calculation.edit: removed Margin b/c it isn't part of ActualWidth.
ActualWidth
由渲染系统设置,并且可能会有所不同,具体取决于其他元素的宽度和总体尺寸限制。 结果是无法改变。Width
是一个可以更改的属性,应用于增加或减少元素的宽度。来自 MSDN:
ActualWidth
is set by the rendering system, and may be different depending on the widths of other elements and overall size constraints. As a result, it can not be changed.Width
is a property that can be changed, and should be used to increase or decrease the width of the element.From MSDN:
有一个很好的理由不使用
ActualWidth
进行绑定(显然相应地使用ActualHeight
)。当您将一个元素的
Width
设置为另一个元素的ActualWidth
时,您可能会破坏布局链。在最好的情况下,您的元素/控件需要在父级(绑定源)的布局过程完成后进行解析。 这意味着额外的时间。
如果它与父级处于同一层次结构级别,则布局过程需要(至少)两次运行才能计算确定的大小。
例如,我有一个控件,其大小属性以将其设置为
TemplatedParent
(不要这样做)的样式重写:当调整包含窗口的大小时,控制将防止容器变小并破坏布局。 将其设置为
Width
将解决问题(do):如果您必须使用
ActualWidth
,通常您的 xaml 有问题。 最好解决这个问题,而不是弄乱布局运行的最终尺寸。There is a very good reason not to use the
ActualWidth
to bind to (obviouslyActualHeight
accordingly).When you set the
Width
of an element, to theActualWidth
of another one you may break the layout chain.In the best case your element/control needs to be parsed after the layout process of the parent (the binding source) finished. That means additional time.
If it is at the same hierarchy level as the parent the layout process needs two runs (at least) to calculate a definitive size.
For example I had a control which had it's size property overridden in a style that would set it to the
TemplatedParent
(don't do):When resizing the containing window, the control would prevent the container from becoming smaller and brake the layout. Setting it to the
Width
will resolve the problem (do):If you have to use the
ActualWidth
in general something is wrong with your xaml. Better fix that instead of messing up with the final sizes of the layout run.正是如此,渲染宽度!=布局宽度。 一种用于布局,另一种用于渲染。 就像 WinForms 一样,有一个 Size 和一个 ClientSize 属性,略有不同,您应该使用渲染的 Atual/Client 大小和布局的宽度/高度。
It's exactly that, the render width != layout width. One is intended to be used for layout the other one is intended for render. It like with WinForms, there was a Size and a ClientSize property, the differ slightly and you should use the Atual/Client size of rendering and the Width/Height for layout.
您可以设置
Width
属性,但不能设置ActualWidth
属性。Width
属性用于确定面板的呈现方式,然后将ActualWidth
设置为所使用的实际宽度。 该值可能与 Width 不同,具体取决于子元素的大小以及父元素的限制。设置
Width
属性时,ActualWidth
不会立即设置,而是会在渲染期间更新(一次或多次)。You can set the
Width
property, but not theActualWidth
property.The
Width
property is used to determine how the panel is rendered, then theActualWidth
is set to the actual width that was used. This may not be the same value as Width, depending on the size of it's child elements and constrictions from it's parent element.The
ActualWidth
is not set immediately when setting theWidth
property, but will be updated (one or more times) during rendering.