Setting Topproperty at runtime from c# RRS feed

  • Question

  • Hi,

    I would like to set the Canvas.Left property (and Canvas.top) of a Label from c# but I keep getting a run-time exception.

    For example I have a label

    <Label x:Name="Label1" Style="{StaticResource styledLabel}" s:Contacts.ContactEnter="LabelDot1_ContactEnter">Text</Label>

    In the c# code I added the line

    Label1.SetValue(Canvas.LeftProperty, 20);

    and the code compiled but I get the error

    '20' is not a valid value for property 'Left' when I run the application.

    I have also tried

                (FindName("Label1") as Label).SetValue(Canvas.LeftProperty, 20); 

    which gives the same runtime error.


    Any ideas where I am going wrong?  I need to be able to position the labels at runtime.


    Thanks in advance.


    Wednesday, July 7, 2010 3:28 PM


  • Silly mistake on my part.

    The value has to be a double.  Casting or using a double which is previously set works.



    • Marked as answer by qwertygal Friday, July 9, 2010 8:06 AM
    Friday, July 9, 2010 8:06 AM