Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/use-automatic-layout-overview.md
T
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

6.3 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Use Automatic Layout Overview 03/30/2017
layout [WPF], automatic
automatic layout [WPF]
6fed9264-18bb-4d05-8867-1fe356c6f687

Use Automatic Layout Overview

This topic introduces guidelines for developers on how to write WPF application design.

Advantages of Using Automatic Layout

Because the WPF presentation system is powerful and flexible, it provides the ability to layout elements in an application that can be adjusted to fit the requirements of different languages. The following list points out some of the advantages of automatic layout.

  • UI displays well in any language.

  • Reduces the need to readjust position and size of controls after text is translated.

  • Reduces the need to readjust window size.

  • UI layout renders properly in any language.

  • Localization can be reduced to the point that it is little more than string translation.

Automatic Layout and Controls

Automatic layout enables an application to adjust the size of a control automatically. For example, a control can change to accommodate the length of a string. This capability enables localizers to translate the string; they no longer need to resize the control to fit the translated text. The following example creates a button with English content.

[!code-xamlLocalizationBtn_snip#1]

In the example, all you have to do to make a Spanish button is change the text. For example,

[!code-xamlLocalizationBtn#1]

The following graphic shows the output of the code samples:

The same button with text in different languages

Automatic Layout and Coding Standards

Using the automatic layout approach requires a set of coding and design standards and rules to produce a fully localizable UI. The following guidelines will aid your automatic layout coding.

Do not use absolute positions

For a discussion about various types of panels, see Panels Overview.

Do not set a fixed size for a window

Add a xref:System.Windows.FrameworkElement.FlowDirection%2A

Use composite fonts instead of physical fonts

Add xml:lang

  • Add the xml:lang attribute in the root element of your UI, such as xml:lang="en-US" for an English application.

  • Because composite fonts use xml:lang to determine what font to use, set this property to support multilingual scenarios.

Automatic Layout and Grids

The xref:System.Windows.Controls.Grid element, is useful for automatic layout because it enables a developer to position elements. A xref:System.Windows.Controls.Grid control is capable of distributing the available space among its child elements, using a column and row arrangement. The UI elements can span multiple cells, and it is possible to have grids within grids. Grids are useful because they enable you to create and position complex UI. The following example demonstrates using a grid to position some buttons and text. Notice that the height and width of the cells are set to xref:System.Windows.GridUnitType.Auto; therefore, the cell that contains the button with an image adjusts to fit the image.

[!code-xamlLocalizationGrid#1]

The following graphic shows the grid produced by the previous code.

Grid example Grid

Automatic Layout and Grids Using the IsSharedSizeScope Property

A xref:System.Windows.Controls.Grid element is useful in localizable applications to create controls that adjust to fit content. However, at times you want controls to maintain a particular size regardless of content. For example, if you have "OK", "Cancel" and "Browse" buttons you probably do not want the buttons sized to fit the content. In this case the xref:System.Windows.Controls.Grid.IsSharedSizeScope%2A?displayProperty=nameWithType attached property is useful for sharing the same sizing among multiple grid elements. The following example demonstrates how to share column and row sizing data between multiple xref:System.Windows.Controls.Grid elements.

[!code-xamlgridIssharedsizescopeProp#2]

Note

For the complete code sample, see Share Sizing Properties Between Grids.

See also