I need to compute the exact height of a RichEditBox, depending on its content. For this sake, I use the following method, which proves quite ok in any case, BUT when the text is one line!
public static double GetElemHeight(FrameworkElement elem, double? actualWidth = null)
{
if (elem == null)
return 0;
// take note of the existing height, if any, since we have to re-establish it later:
double currentH = elem.Height;
if (!double.IsNaN(currentH))
elem.Height = double.NaN;
double totalW = (actualWidth ?? elem.Width) + elem.Margin.Left + elem.Margin.Right;
// Measure() only works as expected in this context if the Height is NaN:
elem.Measure(new Size(totalW, Double.PositiveInfinity));
Size size = elem.DesiredSize;
elem.Height = currentH; //re-establish the correct height
return size.Height - elem.Margin.Top - elem.Margin.Bottom;
}
Basically what happens is that for any text written in the RichEditBox, the method returns the correct height of the element. But when I have a text that covers only one line, the result is always an height that is almost the double of the correct result.
Please find here an MVC that reproduces the problem: https://github.com/cghersi/UWPExamples/tree/master/SizeOfTextBox
Any clue on what am I doing wrong?