This topic has not yet been rated - Rate this topic

FrameworkElement.Width Property

April 12, 2014

Gets or sets the width of a FrameworkElement.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
public double Width { get; set; }
<frameworkElement Width="double"/>
-or-
<frameworkElement Width="Auto"/>

Property Value

Type: System.Double
The width of the element, in logical pixels. The default is Double.NaN. Except for the special Double.NaN value, this value must be equal to or greater than 0. See Remarks for upper bound information.

Dependency property identifier field: WidthProperty

Several of the FrameworkElement derived classes are also derived from Shape. Not all of the Shape classes typically use Height or Width to specify their appearance, and instead use specific properties that might define a set of points. In this case a Height or Width is calculated, but is not typically set directly.

Custom classes might have similar considerations where the class might have properties that are more meaningful for specifying dimensions than are Height or Width. In this case Height or Width will both still be available as members and are settable, even though the desirable behavior might be to not enable Height or Width setting. In critical cases, it might be worth shadowing the Height and Width properties to prevent them from being set directly by consumers of the class.

TextBlock is an example of a container object, where the container displays text within it. The size of that text can influence the size of the rendered TextBlock; therefore, TextBlock might not be honoring the specified Height or Width. For container objects such as TextBlock, you should check two additional properties that expose that calculated size, ActualHeight and ActualWidth.

Negative values for Width are not permitted. Non-integral values for Width are technically permitted. Values are preserved as non-integral values if set that way in XAML or code, but should generally be avoided and are normally rounded by the default layout rounding behavior. See.

In addition to the validation check, there is a nondeterministic upper value bound for Width (this is a very large number, larger than Single.MaxValue but smaller than Double.MaxValue). 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.

"Auto" Layout and Double.NaN

The default value of Height and Width is not 0; it is Double.NaN. Height and Width support the ability to be an unset "Auto" value. Because Height and Width are double values, Double.NaN is used as a special value to represent this "Auto" behavior. The layout system interprets the "Auto" value to generally mean that the object should be sized to the available size in layout, instead of to a specific logical pixel value.

If you want the "Auto" behavior for an object when it is used in layout, leave Height and Width unset at their Double.NaN default value. If you have previously set values and want to reenable the "Auto" behavior, set Height and Width specifically to Double.NaN.

Although not a common practice except for perhaps in visual state templates, you can also explicitly set Height and Width attributes in XAML to be the string Auto instead of a double value. Setting Auto in XAML is a special behavior of the XAML parser, not a TypeConverter behavior.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.