Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-set-the-width-properties-of-an-element.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

3.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Set the Width Properties of an Element 03/30/2017
csharp
vb
width properties [WPF]
Panel control [WPF], width properties of elements
6ee04a9d-63f0-4f5b-a406-0a8cd4c35729

How to: Set the Width Properties of an Element

Example

This example visually shows the differences in rendering behavior among the four width-related properties in [!INCLUDETLA#tla_winclient].

The xref:System.Windows.FrameworkElement class exposes four properties that describe the width characteristics of an element. These four properties can conflict, and when they do, the value that takes precedence is determined as follows: the xref:System.Windows.FrameworkElement.MinWidth%2A value takes precedence over the xref:System.Windows.FrameworkElement.MaxWidth%2A value, which in turn takes precedence over the xref:System.Windows.FrameworkElement.Width%2A value. A fourth property, xref:System.Windows.FrameworkElement.ActualWidth%2A, is read-only, and reports the actual width as determined by interactions with the layout process.

The following [!INCLUDETLA#tla_xaml] examples draw a xref:System.Windows.Shapes.Rectangle element (rect1) as a child of xref:System.Windows.Controls.Canvas. You can change the width properties of a xref:System.Windows.Shapes.Rectangle by using a series of xref:System.Windows.Controls.ListBox elements that represent the property values of xref:System.Windows.FrameworkElement.MinWidth%2A, xref:System.Windows.FrameworkElement.MaxWidth%2A, and xref:System.Windows.FrameworkElement.Width%2A. In this manner, the precedence of each property is visually displayed.

[!code-xamlWidthMinWidthMaxWidth#1]
[!code-xamlWidthMinWidthMaxWidth#2]

The following code-behind examples handle the events that the xref:System.Windows.Controls.Primitives.Selector.SelectionChanged event raises. Each custom method takes the input from the xref:System.Windows.Controls.ListBox, parses the value as a xref:System.Double, and applies the value to the specified width-related property. The width values are also converted to a string and written to various xref:System.Windows.Controls.TextBlock elements (definition of those elements is not shown in the selected XAML).

[!code-csharpWidthMinWidthMaxWidth#3] [!code-vbWidthMinWidthMaxWidth#3]

For the complete sample, see Width Properties Comparison Sample.

See also