--- title: "How to: Set the Width Properties of an Element" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "width properties [WPF]" - "Panel control [WPF], width properties of elements" ms.assetid: 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 [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)]. The 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 value takes precedence over the value, which in turn takes precedence over the value. A fourth property, , is read-only, and reports the actual width as determined by interactions with the layout process. The following [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] examples draw a element (`rect1`) as a child of . You can change the width properties of a by using a series of elements that represent the property values of , , and . In this manner, the precedence of each property is visually displayed. [!code-xaml[WidthMinWidthMaxWidth#1](~/samples/snippets/csharp/VS_Snippets_Wpf/WidthMinWidthMaxWidth/CSharp/Window1.xaml#1)] [!code-xaml[WidthMinWidthMaxWidth#2](~/samples/snippets/csharp/VS_Snippets_Wpf/WidthMinWidthMaxWidth/CSharp/Window1.xaml#2)] The following code-behind examples handle the events that the event raises. Each custom method takes the input from the , parses the value as a , and applies the value to the specified width-related property. The width values are also converted to a string and written to various elements (definition of those elements is not shown in the selected XAML). [!code-csharp[WidthMinWidthMaxWidth#3](~/samples/snippets/csharp/VS_Snippets_Wpf/WidthMinWidthMaxWidth/CSharp/Window1.xaml.cs#3)] [!code-vb[WidthMinWidthMaxWidth#3](~/samples/snippets/visualbasic/VS_Snippets_Wpf/WidthMinWidthMaxWidth/VisualBasic/Window1.xaml.vb#3)] For the complete sample, see [Width Properties Comparison Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Elements/WidthProperties). ## See also - - - - - - - [Panels Overview](panels-overview.md) - [Set the Height Properties of an Element](how-to-set-the-height-properties-of-an-element.md) - [Width Properties Comparison Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Elements/WidthProperties)