I am creating a charting tool in WPF. I dynamicaly add the x labels as strings, however I need to know the string width, so I can properly center in underneath the xTick. Alas, I cant get the width using the myLabel.Width; it returns NaN. Are there any suggestions?
Is there a WPF way instead of estimating with system.Drawing?
Height and Width are only defined if you specify them. If not, they are "NotANumber" as you're seeing.
What you want is the ActualWidth/ActualHeight properties. These values are set by the layout system and represent a very good estimate of the actual laid-out-width of the control. Further, they help you deal with centering beneath a control that may be partially clipped.
Thanks for the great assistance. The labels are now nicely placed! This was my final code.
All the best,
myLabel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
Rect measureRect = new Rect(myLabel.DesiredSize);
//Place it in Canvas
Canvas.SetLeft(myLabel, (pc.x - (measureRect.Width)/2));