WPF 中的 Width 和 ActualWidth 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/607827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Width and ActualWidth in WPF?
提问by Andreas Grech
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
.
我目前正在Panel
WPF 中使用s,我注意到关于Width
和Height
属性,还有另外两个名为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.)
ActualWidth
获取此元素的呈现宽度。这是一个依赖属性。(继承自 FrameworkElement。)
Width
获取或设置元素的宽度。这是一个依赖属性。(继承自 FrameworkElement。)
Reference: MSDN
参考:MSDN
Can anyone point out the differences between the two and when to use either one ?
谁能指出两者之间的区别以及何时使用任何一个?
回答by Muad'Dib
Width
/Height
is the requested or layoutsize. If you set to Auto, then the value is double.NaN
when you access the property in code behind.
Width
/Height
是请求的或布局的大小。如果您设置为 Auto,则该值是double.NaN
您在代码隐藏中访问该属性时的值。
ActualWidth
/ActualHeight
and RenderSize.Width
/RenderSize.Height
both return the element's rendered size, as RenderSizeis of type Size. If you want/need the actual size of the item, then use any of these attributes.
ActualWidth
//ActualHeight
和RenderSize.Width
/RenderSize.Height
都返回元素的渲染大小,因为RenderSize是Size类型。如果您想要/需要商品的实际尺寸,请使用这些属性中的任何一个。
回答by Simon_Weaver
I find ActualWidth
most useful when I want to bind the width or height of one element to another.
ActualWidth
当我想将一个元素的宽度或高度绑定到另一个元素时,我发现它最有用。
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.
在这个简单的例子中,我有两个并排排列的按钮,下面有一个注释,该注释被限制为包含两个按钮的 StackPanel 的宽度。
<StackPanel>
<StackPanel Margin="0,12,0,0" Orientation="Horizontal" Name="buttonPanel" HorizontalAlignment="Left" >
<Button Content="Yes - Arm the missile" FontWeight="Bold" HorizontalAlignment="Left"/>
<Button Content="No - Save the world" HorizontalAlignment="Left" Margin="7,0,0,0"/>
</StackPanel>
<TextBlock Text="Please choose whether you want to arm the missile and kill everybody, or save the world by deactivating the missile."
Width="{Binding Path=ActualWidth,ElementName=buttonPanel}" Margin="0,5,0,0" HorizontalAlignment="Left" TextWrapping="Wrap"/>
</StackPanel>
回答by TWood
ActualWidth
accounts for padding in the value so anytime you need to know that number you can call Actualwidth
instead of width and avoid the calculation.
ActualWidth
考虑填充值,因此您需要知道可以调用的数字Actualwidth
而不是宽度并避免计算。
edit: removed Margin b/c it isn't part of ActualWidth.
编辑:删除了边距 b/c 它不是 ActualWidth 的一部分。
回答by Pascal
There is a very good reason not to use the ActualWidth
to bind to (obviously ActualHeight
accordingly).
When you set the Width
of an element, to the ActualWidth
of another one you may break the layout chain.
有一个很好的理由不使用ActualWidth
to 绑定到(显然ActualHeight
相应地)。当您将Width
一个元素的设置为另一个元素的 时,ActualWidth
您可能会破坏布局链。
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):
例如,我有一个控件,它的 size 属性以将其设置为TemplatedParent
(不要做)的样式覆盖:
<Rectangle DockPanel.Dock="Top" Width="{TemplateBinding ActualWidth}"
Height="1" Fill="#000000"/>
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):
调整包含窗口的大小时,控件会阻止容器变小并阻止布局。将其设置为Width
将解决问题(做):
<Rectangle DockPanel.Dock="Top" Width="{TemplateBinding Width}"
Height="1" Fill="#000000"/>
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.
如果您必须使用ActualWidth
一般的 xaml 有问题。更好地解决这个问题,而不是弄乱布局运行的最终大小。
回答by Andy Mikula
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.
ActualWidth
由渲染系统设置,可能会根据其他元素的宽度和整体尺寸限制而有所不同。因此,它无法更改。Width
是一个可以改变的属性,应该用于增加或减少元素的宽度。
From MSDN:
从MSDN:
This property is a calculated value based on other width inputs, and the layout system. The value is set by the layout system itself, based on an actual rendering pass, and may therefore lag slightly behind the set value of properties such as
Width
that are the basis of the input change.
此属性是基于其他宽度输入和布局系统的计算值。该值由布局系统本身根据实际渲染过程设置,因此可能稍微滞后于作为
Width
输入更改基础的属性的设置值。
回答by John Leidegren
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.
正是这样,渲染宽度!= 布局宽度。一个用于布局,另一个用于渲染。它与 WinForms 一样,有一个 Size 和一个 ClientSize 属性,略有不同,您应该使用渲染的 Atual/Client 大小和布局的 Width/Height 。
回答by Guffa
You can set the Width
property, but not the ActualWidth
property.
您可以设置Width
属性,但不能设置ActualWidth
属性。
The Width
property is used to determine how the panel is rendered, then the ActualWidth
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.
该Width
属性用于确定面板的呈现方式,然后将ActualWidth
其设置为所使用的实际宽度。这可能与 Width 的值不同,这取决于它的子元素的大小和它的父元素的限制。
The ActualWidth
is not set immediately when setting the Width
property, but will be updated (one or more times) during rendering.
在ActualWidth
设定时,不会立即设置Width
渲染过程中的财产,但将被更新(一次或多次)。