Skip to main content
.NET Framework Class Library
CanvasSetTop Method

Sets the value of the CanvasTop attached property for a given dependency object.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
Public Shared Sub SetTop ( _
	element As [%$TOPIC/ms588537_en-us_VS_110_2_0_0_0_0%], _
	length As [%$TOPIC/ms588537_en-us_VS_110_2_0_0_0_1%] _
public static void SetTop(
	[%$TOPIC/ms588537_en-us_VS_110_2_0_1_0_0%] element,
	[%$TOPIC/ms588537_en-us_VS_110_2_0_1_0_1%] length
static void SetTop(
	[%$TOPIC/ms588537_en-us_VS_110_2_0_2_0_0%]^ element, 
	[%$TOPIC/ms588537_en-us_VS_110_2_0_2_0_1%] length
static member SetTop : 
        element:[%$TOPIC/ms588537_en-us_VS_110_2_0_3_0_0%] * 
        length:[%$TOPIC/ms588537_en-us_VS_110_2_0_3_0_1%] -> unit


Type: System.WindowsUIElement

The element to which the property value is written.

Type: SystemDouble

Sets the CanvasTop coordinate of the specified element.


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

The following example creates a ListBox element that has eleven selectable ListBoxItem elements. The SelectionChanged event triggers the ChangeLeft custom method, which the subsequent code block defines.

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

<ListBox Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Width="60" Margin="10,0,0,0" SelectionChanged="ChangeLeft">

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

Private Sub ChangeLeft(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myLengthConverter As New LengthConverter
    Dim db1 As Double = CType(myLengthConverter.ConvertFromString(li.Content.ToString()), Double)
    Canvas.SetLeft(text1, db1)
    Dim st1 As String = CType(myLengthConverter.ConvertToString(Canvas.GetLeft(text1)), String)
    canvasLeft.Text = "Canvas.Left = " + st1
End Sub
private void ChangeLeft(object sender, SelectionChangedEventArgs args)
			ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
			LengthConverter myLengthConverter = new LengthConverter();
			Double db1 = (Double)myLengthConverter.ConvertFromString(li.Content.ToString());
			Canvas.SetLeft(text1, db1);
			String st1 = (String)myLengthConverter.ConvertToString(Canvas.GetLeft(text1));
			canvasLeft.Text = "Canvas.Left = " + st1;

More Code

How to: Create and Use a CanvasThis example shows how to create and use an instance of Canvas.
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.