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 - Canvas.GetLeft is NaN

 
 
Old 06-06-2006   #1 (permalink)
NetronProject@gmail.com


 
 

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
}


My System SpecsSystem Spec
Old 06-07-2006   #2 (permalink)
Michael Jacobs [MSFT]


 
 

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
> }
>



My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
group elements in canvas .NET General
Canvas does not allow drawing Vista 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