![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| group elements in canvas | .NET General | |||
| Canvas does not allow drawing | Vista General | |||