Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 3.58 KB

how-to-set-the-height-properties-of-an-element.md

File metadata and controls

43 lines (33 loc) · 3.58 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Set the Height Properties of an Element
Learn how to set the height properties of an element in a Windows Presentation Foundation (WPF) application.
03/30/2017
csharp
vb
height properties [WPF]
Panel control [WPF], height properties of elements
5ab9e781-dbb8-469a-a3c8-cf38ce312647

How to: Set the Height Properties of an Element

Example

This example visually shows the differences in rendering behavior among the four height-related properties in Windows Presentation Foundation (WPF).

The xref:System.Windows.FrameworkElement class exposes four properties that describe the height 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.MinHeight%2A value takes precedence over the xref:System.Windows.FrameworkElement.MaxHeight%2A value, which in turn takes precedence over the xref:System.Windows.FrameworkElement.Height%2A value. A fourth property, xref:System.Windows.FrameworkElement.ActualHeight%2A, is read-only, and reports the actual height as determined by interactions with the layout process.

The following Extensible Application Markup Language (XAML) examples draw a xref:System.Windows.Shapes.Rectangle element (rect1) as a child of xref:System.Windows.Controls.Canvas. You can change the height 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.MinHeight%2A, xref:System.Windows.FrameworkElement.MaxHeight%2A, and xref:System.Windows.FrameworkElement.Height%2A. In this manner, the precedence of each property is visually displayed.

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

The following code-behind examples handle the events that the xref:System.Windows.Controls.Primitives.Selector.SelectionChanged event raises. Each handler 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 height-related property. The height 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-csharpHeightMinHeightMaxHeight#3] [!code-vbHeightMinHeightMaxHeight#3]

For the complete sample, see Height Properties Sample.

See also