Edit

Share via


FrameworkElement.Width Property

Definition

Gets or sets the width of the element.

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

Property Value

The width of the element, in device-independent units (1/96th inch per unit). The default value is NaN. This value must be equal to or greater than 0.0. See Remarks for upper bound information.

Attributes

Remarks

This is one of three properties on FrameworkElement that specify width information. The other two are MinWidth and MaxWidth. If 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. In contrast, the value of the ActualWidth may vary. The layout may have rejected the suggested size for some reason. 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.

In addition to acceptable Double values, this property can also be Double.NaN. This is how you specify auto sizing behavior. In XAML 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.

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

<object Width="double"/>  

-or-

<object Width ="qualifiedDouble"/>  

-or-

<object Width ="Auto"/>  

XAML Values

double
Double

String representation of a Double value equal to or greater than 0.0. See Remarks for upper bound information. This value is interpreted as a device-independent unit (1/96th inch) measurement. Strings need not explicitly include decimal points. For instance a value of 1 is acceptable.

qualifiedDouble
A double value as described above, followed by one of the following unit declaration strings: px, in, cm, pt.

px (default) is device-independent units (1/96th inch per unit)

in is inches; 1in==96px

cm is centimeters; 1cm==(96/2.54) px

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

Auto
Enables autosizing behavior. See Remarks.

Dependency Property Information

Identifier field WidthProperty
Metadata properties set to true AffectsMeasure

Applies to