Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - How to get the coordinates of a FrameworkElement?

 
 
Old 01-31-2006   #1 (permalink)
Jason Dolinger


 
 

How to get the coordinates of a FrameworkElement?

In my continuing series of questions for the day, is there a way to get
the coordinates of a FrameworkElement (assuming it is somewhat
rectangular in shape?). There is a method Visual.getContentBounds(),
which returns you the bounding Rect, but it's unfortunately an internal
method.

I'd assume other people would like to do this as well? I basically have
a layered Grid AND Canvas. There are FrameworkElements layed out in the
Grid, and when they are moused over I need to display some things
somewhere in the region above them, and I'm choosing to layout those out
in the Canvas.

Thanks, any ideas?

Jason

My System SpecsSystem Spec
Old 01-31-2006   #2 (permalink)
Michael Latta


 
 

Re: How to get the coordinates of a FrameworkElement?

You can use VisualHelper.GetContentBounds to get the same rectangle. But,
that rectangle is relative to the parent element. To get screen coordinates
you would need to walk up the element tree converting the original rectangle
as you go. You can do this until you get to an element with the same
coordinates as your canvas. I have had problems with the Visual method that
transforms a point from one coordinate space to another. Getting the
transform of each visual in the tree and transforming one at a time seems to
work more reliably in current CTPs.

Michael


"Jason Dolinger" <jdolinger@lab49.com> wrote in message
news:uxlaiQgIGHA.312@TK2MSFTNGP09.phx.gbl...
> In my continuing series of questions for the day, is there a way to get
> the coordinates of a FrameworkElement (assuming it is somewhat rectangular
> in shape?). There is a method Visual.getContentBounds(), which returns
> you the bounding Rect, but it's unfortunately an internal method.
>
> I'd assume other people would like to do this as well? I basically have a
> layered Grid AND Canvas. There are FrameworkElements layed out in the
> Grid, and when they are moused over I need to display some things
> somewhere in the region above them, and I'm choosing to layout those out
> in the Canvas.
>
> Thanks, any ideas?
>
> Jason



My System SpecsSystem Spec
Old 01-31-2006   #3 (permalink)
Bob Brown[MSFT]


 
 

Re: How to get the coordinates of a FrameworkElement?

I did this and it makes a little caption move left <--> right just above my
FE according to the mouse's X position. Just hook it up to the FE's that
you want to add this behavior to. I'm not sure if this is what you were
looking for... I don't know what you meant by "the coordinates of a
FrameworkElement". I assumed that you meant the top left corner of its
bounding rect.

private void OnMouseMove( object sender, MouseEventArgs args )
{
// get mouse position relative to window.
Point windowOffset = args.GetPosition( this );

// get mouse position relative to "sender" in the grid
Point elementOffset = args.GetPosition(
(FrameworkElement)sender );

// calculate the difference between the two
Vector offset = windowOffset - elementOffset;

// create a new element and position it on the canvas above
"sender"
TextBlock text = new TextBlock();
text.Text = "Caption";
text.SetValue( Canvas.LeftProperty, windowOffset.X ); // put it
at mouse's x position
text.SetValue( Canvas.TopProperty, offset.Y - 30 ); // 30px
margin above "sender"

// replace the old caption with the new one
canvas.Children.Clear();
canvas.Children.Add( text );
}

--
Bob Brown [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.


"Jason Dolinger" <jdolinger@lab49.com> wrote in message
news:uxlaiQgIGHA.312@TK2MSFTNGP09.phx.gbl...
> In my continuing series of questions for the day, is there a way to get
> the coordinates of a FrameworkElement (assuming it is somewhat rectangular
> in shape?). There is a method Visual.getContentBounds(), which returns
> you the bounding Rect, but it's unfortunately an internal method.
>
> I'd assume other people would like to do this as well? I basically have a
> layered Grid AND Canvas. There are FrameworkElements layed out in the
> Grid, and when they are moused over I need to display some things
> somewhere in the region above them, and I'm choosing to layout those out
> in the Canvas.
>
> Thanks, any ideas?
>
> Jason



My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Store Program Coordinates .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46