If you set TextWrapping to \"Wrap\", a WPF TextBlock can have several lines of text. Is there a \"clean\" way to get the number of lines of text? I considered looking at the
I have seen that this question is already 7 years old, but I just came with a solution:
The TextBlock have a private property called LineCount. I created an extension method to read this value:
public static class TextBlockExtension
{
public static int GetLineCount(this TextBlock tb)
{
var propertyInfo = GetPrivatePropertyInfo(typeof(TextBlock), "LineCount");
var result = (int)propertyInfo.GetValue(tb);
return result;
}
private static PropertyInfo GetPrivatePropertyInfo(Type type, string propertyName)
{
var props = type.GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic);
return props.FirstOrDefault(propInfo => propInfo.Name == propertyName);
}
}