Change Canvas.Left property in code behind?

Edward Tanguay picture Edward Tanguay · Feb 12, 2009 · Viewed 52k times · Source

I have a rectangle in my XAML and want to change its Canvas.Left property in code behind:

<UserControl x:Class="Second90.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" KeyDown="txt_KeyDown">
    <Canvas>
        <Rectangle 
            Name="theObject" 
            Canvas.Top="20" 
            Canvas.Left="20" 
            Width="10" 
            Height="10" 
            Fill="Gray"/>
    </Canvas>
</UserControl>

But this doesn't work:

private void txt_KeyDown(object sender, KeyEventArgs e)
{
    theObject.Canvas.Left = 50;
}

Does anyone know what the syntax is to do this?

Answer

AnthonyWJones picture AnthonyWJones · Feb 12, 2009
Canvas.SetLeft(theObject, 50)