Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-get-or-set-canvas-positioning-properties.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.0 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Get or Set Canvas Positioning Properties 03/30/2017
csharp
vb
Canvas control [WPF], setting positioning properties
1636b950-2b5a-4507-8a10-c5034cc58b1c

How to: Get or Set Canvas Positioning Properties

This example shows how to use the positioning methods of the xref:System.Windows.Controls.Canvas element to position child content. This example uses content in a xref:System.Windows.Controls.ListBoxItem to represent positioning values and converts the values into instances of xref:System.Double, which is a required argument for positioning. The values are then converted back into strings and displayed as text in a xref:System.Windows.Controls.TextBlock element by using the xref:System.Windows.Controls.Canvas.GetLeft%2A method.

Example

The following example creates a xref:System.Windows.Controls.ListBox element that has eleven selectable xref:System.Windows.Controls.ListBoxItem elements. The xref:System.Windows.Controls.Primitives.Selector.SelectionChanged event triggers the ChangeLeft custom method, which the subsequent code block defines.

Each xref:System.Windows.Controls.ListBoxItem represents a xref:System.Double value, which is one of the arguments that the xref:System.Windows.Controls.Canvas.SetLeft%2A method of xref:System.Windows.Controls.Canvas accepts. In order to use a xref:System.Windows.Controls.ListBoxItem to represent an instance of xref:System.Double, you must first convert the xref:System.Windows.Controls.ListBoxItem to the correct data type.

[!code-xamlCanvasPositioningProperties#1]

When a user changes the xref:System.Windows.Controls.ListBox selection, it invokes the ChangeLeft custom method. This method passes the xref:System.Windows.Controls.ListBoxItem to a xref:System.Windows.LengthConverter object, which converts the xref:System.Windows.Controls.ContentControl.Content%2A of a xref:System.Windows.Controls.ListBoxItem to an instance of xref:System.Double (notice that this value has already been converted to a xref:System.String by using the xref:System.Windows.Controls.Control.ToString%2A method). This value is then passed back to the xref:System.Windows.Controls.Canvas.SetLeft%2A and xref:System.Windows.Controls.Canvas.GetLeft%2A methods of xref:System.Windows.Controls.Canvas in order to change the position of the text1 object.

[!code-csharpCanvasPositioningProperties#2] [!code-vbCanvasPositioningProperties#2]

See also