views:

150

answers:

1

I am not able to use SetTop property Canvas in Silverlight App. Following is the minimal code to reproduce the problem

from System import TimeSpan
from System.Windows import Application, Duration, PropertyPath
from System.Windows.Controls import Canvas, TextBlock
from System.Windows.Media import SolidColorBrush, Colors
from System.Windows.Media.Animation import (
    DoubleAnimation, Storyboard, RepeatBehavior
)

root = Canvas()

root.Children.Clear()
root.Resources.Clear()
#root.Background = SolidColorBrush(Colors.Black)

parent = Canvas(Width = 100, Height = 100)
parent.Background = SolidColorBrush(Colors.Black)
parent.SetTop(root, 0)
parent.SetLeft(root, 0)

parent2 = Canvas(Width = 100, Height = 100)
parent2.Background = SolidColorBrush(Colors.Blue)
parent2.SetTop(root, 100)

root.Children.Add(parent)

Application.Current.RootVisual = root


The line "parent2.SetTop(root, 100)" changes the position where parent1 will be displayed.

This behavior is surprising. I am not even adding parent2 as one of the child object still it's affecting the layout.

Can somebody please explain what I am doing wrong?

+3  A: 

I think you're using SetTop "backwards". SetTop sets the attached property on a child control of the Canvas so doing this:

parent2.SetTop(root,100)

Sets the Canvas.Top property of root to be 100 (not parent2) (so will hence move all of the children of root too). What you should be doing is:

root.SetTop(parent, 0)

Which will set the Canvas.Top property of parent to be 0.

More info on MSDN.

Steven Robbins
Many Thanks. It's surprising how such small things can cause major frustration. Thanks again.
Manish
No worries - attached properties can be somewhat confusing :-)
Steven Robbins