tags:

views:

13

answers:

1

I'm new to Silverlight programming, and the DependencyProperty is still relatively new to me, which causes the following issue:

I'd like to attach to an event, when Canvas.LeftProperty of Canvas.TopProperty changes for an UIElement (a UserControl in my case).

For example, I'm able to do the following:

        source.SizeChanged += delegate
            {
                target.Width = source.Width;
                target.Height = source.Height;
            };

But I was unable to find a similar event for location. Is this even possible?

+1  A: 

An element does not generally know about it's location in the WPF layout system. The parent element (in your case, a Canvas) is responsible for laying out the element and the element is simply responsible for sizing itself according to the constraints of the layout container.

Having said that, you can use the LayoutUpdated event on the element. It is probably best to set this on the panel and use it as a trigger to re-scan the properties of the children since the LayoutUpdated event (according to the docs) always passes a null reference to the sender parameter.

Josh Einstein
I had already tried LayoutUpdated. Unfortunately it's only triggered once at the initial startup.The current workaround is having the place that updates source, to update target as well. However this introduces an unnecessary dependency to the code.
sukru
What about binding? target.SetBinding(FrameworkElement.WidthProperty, new Binding("Width") { Source = source });
Josh Einstein
Thanks, that's a step in the right direction. But Left and Top properties will actually involve some calculation (base + offset). At least I'm learning more about these properties (I still miss the simplicity of Windows.Forms)
sukru