8

Say I have a StackPanel that gets dynamically filled with copy, changing the Y position of elements inside it. I have a specific element within that StackPanel that I want to find the Y position of (relative to the StackPanel or otherwise) after the StackPanel is done repositioning all of it's children.

StackPanel sp = new StackPanel();
sp.Children.Add(someTextBlock);
sp.Children.Add(element1);
sp.Children.Add(element2);
...
someTextBlock.Text = "Lorem ipsum dolor..." // some text that pushes children of > index down
// element1 got pushed down to some unknown position based on text length
// now want to find the Y position of element1

I noticed that methods like this: http://forums.silverlight.net/forums/p/16787/55881.aspx#55881 don't work since the position returned is the position of the StackPanel and not the child element I'm targeting.

4

2 に答える 2

14

投稿したリンクのメソッドは、正しく呼び出せば正常に動作するはずです。

正しい UIElement でそれらを呼び出す必要があります。この場合、element1 を RootVisual に使用すると、次の完全な位置が得られますelement1

var transform = element1.TransformToVisual(Application.Current.RootVisual as FrameworkElement);        
Point absolutePosition = transform.Transform(new Point(0, 0));
于 2009-10-02T17:15:34.043 に答える