Getting the line count of a wrapped TextBlock in WPF

OK, so TextBlock does have a LineCount property, the only problem is, it is a private Property. It seems like half the private and internal fields should be public or at least protected so the children can get at them.

Rant: Sometimes I shake my head at the decisions made by the original developers, then I remember WPF is awesome and these little implementation mistakes aren’t always a big deal. Still, if they would just open source WPF, we could submit such a fix in seconds.

Well, let’s get around this. Did you know that you can access private fields and properties using reflection? You can. Simply follow the steps in this post:

How to get and set private fields or properties in C#

We can use reflection to get the LineCount value. In the article, they use a PrivateValueAccessor static class. However, for this purpose, I will simply take the needed method.

Here is a child class of TextBlock that gets LineCount. It also updates the LineCount on Loaded and on SizeChanged, as the LineCount is zero before the control is loaded. Once it is loaded a resize could change the line count.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Rhyous.TextBlock.Controls
{
    public class TextBlock2 : System.Windows.Controls.TextBlock, INotifyPropertyChanged
    {
        public const string LineCountPropertyName = "LineCount";

        public TextBlock2()
        {
            Loaded += OnLoaded;
            SizeChanged += TextBlock2_SizeChanged;
        }

        public int LineCount
        {
            get { return (int)GetPrivatePropertyInfo(typeof(System.Windows.Controls.TextBlock), LineCountPropertyName).GetValue(this); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                PropertyChanged(this, e);
            }
        }

        private void TextBlock2_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            NotifyPropertyChanged(LineCountPropertyName);
        }

        private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            NotifyPropertyChanged(LineCountPropertyName);
        }

        private BindingFlags Flags = BindingFlags.Instance
                                   | BindingFlags.GetProperty
                                   | BindingFlags.NonPublic;

        private PropertyInfo GetPrivatePropertyInfo(Type type, string propertyName)
        {
            var props = type.GetProperties(Flags);
            return props.FirstOrDefault(propInfo => propInfo.Name == propertyName);
        }
    }
}

Here is an example where I use it. I added to additional controls, so you can see that the LineCount is 0 before render and 5 after render.

    <controls:TextBlock2 x:Name="Tb2" TextWrapping="Wrap" MaxWidth="100" HorizontalAlignment="Left"
                         Text="Some very long text that absolutely must demonstrate wrapping." />
    <TextBlock Name="BeforeRender2" Text="{Binding LineCount, ElementName=Tb2, StringFormat='TextBox2 Line Count before render: {0}', Mode=OneTime}"/>
    <TextBlock Name="AfterRender2"  Text="{Binding LineCount, ElementName=Tb2, StringFormat='TextBox2 Line Count after render: {0}'}" />

Leave a Reply

*