![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 } |
| | #2 (permalink) |
| 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 > } > |
| |
| |
![]() |
| 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 |