FrameworkElement.Width Property


获取或设置元素的宽度。Gets or sets the width of the element.

 property double Width { double get(); void set(double value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public double Width { get; set; }
member this.Width : double with get, set
Public Property Width As Double

Property Value


元素的宽度(以 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit) 为单位)。The width of the element, in 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit). 默认值是 NaNThe default value is NaN. 此值必须等于或大于 0.0。This value must be equal to or greater than 0.0. 请参阅“备注”以了解上限信息。See Remarks for upper bound information.



这是 FrameworkElement 中指定宽度信息的三个属性之一。This is one of three properties on FrameworkElement that specify width information. 另外两个 MinWidthMaxWidthThe other two are MinWidth and MaxWidth. 如果这两个值之间存在冲突,则必须先执行实际的宽度确定的应用程序顺序,然后 MaxWidth,最后,如果其中每个 MinWidth 都在界限内,WidthIf there is a conflict between these values, the order of application for actual width determination is first MinWidth must be honored, then MaxWidth, and finally if each of these are within bounds, Width.

此属性的返回值始终与任何设置为它的值相同。The return value of this property is always the same as any value that was set to it. 相反,ActualWidth 的值可能会有所不同。In contrast, the value of the ActualWidth may vary. 出于某种原因,布局可能已拒绝建议的大小。The layout may have rejected the suggested size for some reason. 此外,布局系统本身相对于 Width 的属性系统集以异步方式运行,并且可能尚未处理特定大小调整属性的更改。Also, the layout system itself works asynchronously relative to the property system set of Width and may not have processed that particular sizing property change yet.

除了可接受的 Double 值外,还可以 Double.NaN此属性。In addition to acceptable Double values, this property can also be Double.NaN. 这是指定自动调整大小行为的方式。This is how you specify auto sizing behavior. XAMLXAML 将值设置为字符串 "Auto" (不区分大小写)以启用自动调整大小行为。In XAMLXAML you set the value to the string "Auto" (case insensitive) to enable the auto sizing behavior. 自动调整大小行为表示元素将填充其可用的宽度。Auto sizing behavior implies that the element will fill the width available to it. 但请注意,特定控件经常以默认样式提供默认值,这些默认值将禁用自动调整大小行为,除非专门重新启用。Note however that specific controls frequently supply default values in their default styles that will disable the auto sizing behavior unless it is specifically re-enabled.

除了验证检查,对于布局系统强制执行的 Width 还有一个不确定的上限值(这是一个非常大的数字,大于 Single.MaxValue 但小于 Double.MaxValue)。In addition to the validation check, there is a nondeterministic upper value bound for Width that is enforced by the layout system (this is a very large number, larger than Single.MaxValue but smaller than Double.MaxValue). 如果超出此限制,元素将不会呈现,并且不会引发异常。If you exceed this bound, the element will not render, and no exception is thrown. 不要将 Width 设置为远远大于任何可能的视觉对象显示大小的最大大小的值,否则可能会超出此不确定性的上限。Do not set Width to a value that is significantly larger than the maximum size of any possible visual display, or you may exceed this nondeterministic upper bound.

XAML 特性用法XAML Attribute Usage

<object Width="double"/>  


<object Width ="qualifiedDouble"/>  


<object Width ="Auto"/>  



大于或等于 0.0 Double 值的字符串表示形式。String representation of a Double value equal to or greater than 0.0. 请参阅“备注”以了解上限信息。See Remarks for upper bound information. 此值被解释为 与设备无关的单位(1/96 英寸)device-independent unit (1/96th inch) 度量。This value is interpreted as a 与设备无关的单位(1/96 英寸)device-independent unit (1/96th inch) measurement. 字符串不需要显式包含小数点。Strings need not explicitly include decimal points. 例如,1 的值是可接受的。For instance a value of 1 is acceptable.

如上所述的双精度值,后跟以下单元声明字符串之一: pxincm``ptA double value as described above, followed by one of the following unit declaration strings: px, in, cm, pt.

px (默认值)为 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)px (default) is 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)

in 为英寸;1in = = 96pxin is inches; 1in==96px

cm 是厘米;1cm = = (96/2.54) pxcm is centimeters; 1cm==(96/2.54) px

pt 为点;1pt = = (96/72) pxpt is points; 1pt==(96/72) px

启用自动调整行为。Enables autosizing behavior. 请参阅“备注”。See Remarks.

依赖项属性信息Dependency Property Information

标识符字段Identifier field WidthProperty
元数据属性设置为 trueMetadata properties set to true AffectsMeasure

Applies to