This documentation is archived and is not being maintained.

How to: Instantiate and Use a WrapPanel in Code

Example

The following code example demonstrates how to instantiate and use a WrapPanel using code. Three Button elements are added as Children of the WrapPanel. The Button elements wrap to the next line when they reach the edge of the WrapPanel.


// Instantiate a new WrapPanel and set properties
myWrapPanel = new WrapPanel();
myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.ItemHeight = 25;

myWrapPanel.ItemWidth = 75;
myWrapPanel.Width = 150;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;

// Define 3 button elements. Each button is sized at width of 75, so the third button wraps to the next line.
btn1 = new Button();
btn1.Content = "Button 1";
btn2 = new Button();
btn2.Content = "Button 2";
btn3 = new Button();
btn3.Content = "Button 3";

// Add the buttons to the parent WrapPanel using the Children.Add method.
myWrapPanel.Children.Add(btn1);
myWrapPanel.Children.Add(btn2);
myWrapPanel.Children.Add(btn3);

// Add the WrapPanel to the MainWindow as Content
mainWindow.Content = myWrapPanel;
mainWindow.Show();

See Also

Reference

WrapPanel

Concepts

Panels Overview

Show: