WPF 中的宽度和实际宽度有什么区别?

发布于 2024-07-14 02:07:22 字数 568 浏览 9 评论 0原文

我目前正在 WPF 中使用 Panel,我注意到关于 WidthHeight 属性,还有另外两个属性,称为实际宽度实际高度

实际宽度

获取此元素的渲染宽度 元素。 这是一个依赖 财产。 (继承自 框架元素。)

宽度

获取或设置元素的宽度。 这是一个依赖属性。 (继承自FrameworkElement。)

参考:MSDN< /a>

谁能指出两者之间的差异以及何时使用其中之一?

I am currently working with Panels 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 技术交流群。

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

发布评论

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

评论(7

自此以后,行同陌路 2024-07-21 02:07:22

Width/Height请求的或布局的尺寸。 如果您设置为 Auto,则当您在代码隐藏中访问该属性时,该值为 double.NaN

ActualWidth/ActualHeightRenderSize.Width/RenderSize.Height 均返回元素的渲染大小,如 RenderSize 的类型为Size。 如果您想要/需要项目的实际大小,请使用这些属性中的任何一个。

Width/Height is the requested or layout size. If you set to Auto, then the value is double.NaN when you access the property in code behind.

ActualWidth/ActualHeight and RenderSize.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.

九公里浅绿 2024-07-21 02:07:22

当我想将一个元素的宽度或高度绑定到另一个元素时,我发现 ActualWidth 最有用。

在这个简单的示例中,我有两个并排排列的按钮,下面的注释受限于包含这两个按钮的 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>

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.

<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>
萧瑟寒风 2024-07-21 02:07:22

ActualWidth 考虑了值中的填充,因此任何时候您需要知道该数字时,都可以调用 Actualwidth 而不是宽度并避免计算。

编辑:删除了边距,因为它不是实际宽度的一部分。

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.

edit: removed Margin b/c it isn't part of ActualWidth.

[浮城] 2024-07-21 02:07:22

ActualWidth 由渲染系统设置,并且可能会有所不同,具体取决于其他元素的宽度和总体尺寸限制。 结果是无法改变。 Width 是一个可以更改的属性,应用于增加或减少元素的宽度。

来自 MSDN

此属性是基于其他宽度输入和布局系统的计算值。 该值是由布局系统本身根据实际渲染通道设置的,因此可能会稍微落后于作为输入更改基础的属性(例如 Width)的设置值。

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:

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.

玉环 2024-07-21 02:07:22

有一个很好的理由不使用ActualWidth进行绑定(显然相应地使用ActualHeight)。
当您将一个元素的Width设置为另一个元素的ActualWidth时,您可能会破坏布局链

在最好的情况下,您的元素/控件需要在父级(绑定源)的布局过程完成后进行解析。 这意味着额外的时间。
如果它与父级处于同一层次结构级别,则布局过程需要(至少)两次运行才能计算确定的大小。

例如,我有一个控件,其大小属性以将其设置为 TemplatedParent (不要这样做)的样式重写:

<Rectangle DockPanel.Dock="Top" Width="{TemplateBinding ActualWidth}" 
           Height="1" Fill="#000000"/>

当调整包含窗口的大小时,控制将防止容器变小并破坏布局。 将其设置为 Width 将解决问题(do)

<Rectangle DockPanel.Dock="Top" Width="{TemplateBinding Width}" 
           Height="1" Fill="#000000"/>

如果您必须使用 ActualWidth,通常您的 xaml 有问题。 最好解决这个问题,而不是弄乱布局运行的最终尺寸。

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.

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):

<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):

<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.

雪花飘飘的天空 2024-07-21 02:07:22

正是如此,渲染宽度!=布局宽度。 一种用于布局,另一种用于渲染。 就像 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.

你怎么敢 2024-07-21 02:07:22

您可以设置 Width 属性,但不能设置 ActualWidth 属性。

Width 属性用于确定面板的呈现方式,然后将 ActualWidth 设置为所使用的实际宽度。 该值可能与 Width 不同,具体取决于子元素的大小以及父元素的限制。

设置 Width 属性时,ActualWidth 不会立即设置,而是会在渲染期间更新(一次或多次)。

You can set the Width property, but not the ActualWidth property.

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.

The ActualWidth is not set immediately when setting the Width property, but will be updated (one or more times) during rendering.

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