UIElement.DesiredSize UIElement.DesiredSize UIElement.DesiredSize UIElement.DesiredSize Property

定义

获取在布局流程的度量传递过程中此元素计算所得的大小。Gets the size that this element computed during the measure pass of the layout process.

public:
 property System::Windows::Size DesiredSize { System::Windows::Size get(); };
public System.Windows.Size DesiredSize { get; }
member this.DesiredSize : System.Windows.Size
Public ReadOnly Property DesiredSize As Size

属性值

计算所得大小,它将成为排列过程所需的大小。The computed size, which becomes the desired size for the arrange pass.

示例

下面的示例显示DesiredSize作为MeasureOverride实现的一部分。The following example shows DesiredSize as part of a MeasureOverride implementation. 请注意Measure在获取DesiredSize之前如何立即调用。Notice how Measure is called immediately prior to obtaining DesiredSize. 这可DesiredSize确保持有合法值。This assures that DesiredSize holds a legitimate value.

virtual Size MeasureOverride(Size availableSize) override
{
    Size^ panelDesiredSize = gcnew Size();

    // In our example, we just have one child. 
    // Report that our panel requires just the size of its only child.
    for each (UIElement^ child in InternalChildren)
    {
        child->Measure(availableSize);
        panelDesiredSize = child->DesiredSize;
    }
    return *panelDesiredSize ;
}
protected override Size MeasureOverride(Size availableSize)
{
    Size panelDesiredSize = new Size();

    // In our example, we just have one child. 
    // Report that our panel requires just the size of its only child.
    foreach (UIElement child in InternalChildren)
    {
        child.Measure(availableSize);
        panelDesiredSize = child.DesiredSize;
    }

    return panelDesiredSize ;
}
Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
    Dim panelDesiredSize As Size = New Size()
    ' In our example, we just have one child. 
    ' Report that our panel requires just the size of its only child.
    For Each child As UIElement In InternalChildren
        child.Measure(availableSize)
        panelDesiredSize = child.DesiredSize
    Next
    Return panelDesiredSize
End Function

注解

如果IsMeasureValid属性的值为, true则此属性返回的值将只是有效的度量值。The value returned by this property will only be a valid measurement if the value of the IsMeasureValid property is true.

DesiredSize当你实现布局行为重写 (如、或ArrangeOverride OnRender ) 时, 通常会将其MeasureOverride作为度量因素之一进行OnRender检查 (在这种情况下, 你可能会改为选中RenderSize , 但这取决于你的实现)。DesiredSize is typically checked as one of the measurement factors when you implement layout behavior overrides such as ArrangeOverride, MeasureOverride, or OnRender (in the OnRender case, you might check RenderSize instead, but this depends on your implementation). 根据方案DesiredSize的不同, 可能会完全遵守实现逻辑, DesiredSize可能会应用约束, 并且此类约束也可能会更改父元素或子元素的其他特征。Depending on the scenario, DesiredSize might be fully respected by your implementation logic, constraints on DesiredSize might be applied, and such constraints might also change other characteristics of either the parent element or child element. 例如, 支持可滚动区域 (但选择不从已启用可滚动区域的 WPF 框架级控件派生) 的控件可以将DesiredSize可用大小与进行比较。For example, a control that supports scrollable regions (but chooses not to derive from the WPF framework-level controls that already enable scrollable regions) could compare available size to DesiredSize. 然后, 控件可以设置在的中UIUI为该控件启用滚动条的内部状态。The control could then set an internal state that enabled scrollbars in the UIUI for that control. 或者, DesiredSize在某些情况下, 也可能会被忽略。Or, DesiredSize could potentially also be ignored in certain scenarios.

适用于

另请参阅