Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Canvas.GetLeft is NaN

Closed Thread
 
Thread Tools Display Modes
Old 06-06-2006   #1 (permalink)
NetronProject@gmail.com
Guest


 

Canvas.GetLeft is NaN

I have a custom FrameworkElement which is added via code to a canvas.
Whenever I ask the canvas the position of the element via
Canvas.GetLeft(...) I get a NaN.

What is wrong?


The custom element is defined as

public class CustomRender : FrameworkElement
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);

drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0,
0, 100, 50));

FormattedText text = new FormattedText("Whatever",
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface("Verdana"), 12, Brushes.Black);
drawingContext.DrawText(text, new Point(23, 23));
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(100, 50);
}
}

while the addition is done as:

{
...
cr = new CustomRender();
canvas.Children.Add(cr);
cr.MouseDown += new MouseButtonEventHandler(cr_MouseDown);
...
}


void cr_MouseDown(object sender, MouseButtonEventArgs e)
{
double left = Canvas.GetLeft(e.Source as UIElement); //gives NaN
}

Old 06-07-2006   #2 (permalink)
Michael Jacobs [MSFT]
Guest


 

Re: Canvas.GetLeft is NaN

Canvas.GetLeft is returning NaN (the default value for that property)
because, as far as I can tell, you never set the Canvas.Left property on
your CustomRender object. Until you give the object a Canvas.Left value
using the Canvas.SetLeft method, it will continue to return NaN.

It seems like the question you want to answer is "where did this thing end
up?" To determine the position of a Visual relative to its parent, you can
use the VisualTreeHelper.GetOffset method.

Vector offset = VisualTreeHelper.GetOffset(cr);

Thanks,

Mike Jacobs [MSFT]


<NetronProject@gmail.com> wrote in message
news:1149623142.710923.298030@g10g2000cwb.googlegroups.com...
>I have a custom FrameworkElement which is added via code to a canvas.
> Whenever I ask the canvas the position of the element via
> Canvas.GetLeft(...) I get a NaN.
>
> What is wrong?
>
>
> The custom element is defined as
>
> public class CustomRender : FrameworkElement
> {
> protected override void OnRender(DrawingContext drawingContext)
> {
> base.OnRender(drawingContext);
>
> drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0,
> 0, 100, 50));
>
> FormattedText text = new FormattedText("Whatever",
> CultureInfo.CurrentUICulture,
> FlowDirection.LeftToRight,
> new Typeface("Verdana"), 12, Brushes.Black);
> drawingContext.DrawText(text, new Point(23, 23));
> }
> protected override Size MeasureOverride(Size availableSize)
> {
> return new Size(100, 50);
> }
> }
>
> while the addition is done as:
>
> {
> ...
> cr = new CustomRender();
> canvas.Children.Add(cr);
> cr.MouseDown += new MouseButtonEventHandler(cr_MouseDown);
> ...
> }
>
>
> void cr_MouseDown(object sender, MouseButtonEventArgs e)
> {
> double left = Canvas.GetLeft(e.Source as UIElement); //gives NaN
> }
>



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Canvas does not allow drawing PhredBear Vista General 2 3 Weeks Ago 02:07 PM
Disposable Canvas? Jon Davis Avalon 26 05-21-2007 09:35 AM
C#: Canvas.Loaded not firing znelson Avalon 2 03-23-2007 10:17 AM
Focusable Canvas =?Utf-8?B?Sm9obiBEdW5u?= Avalon 1 07-31-2006 03:30 PM
3D, Canvas, and Databinding Christopher Bennage Avalon 5 05-31-2006 02:34 PM








Vistax64.com 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 2005-2008

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 47 48 49 50