Archive for the ‘Controls’ Category.

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}'}" />

A FileTextBox control in WPF

Have you ever wanted a text file to be displayed in readonly fashion in a WPF window? Perhaps you have a log file and you want the UI to stay synced with the log updates to the file? Maybe you simply want to write a WPF version of WinTail?

Well, I just wrote a control in which you can set file path to a DependencyProperty and the UI will stay up to date. I created a child of TextBox called FileTextBox and implemented FileSystemWatcher.

Update: 6/16/2014 – Added AutoScroll.

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace WpfSharp.UserControls
{
    public class FileTextBox : TextBox
    {
        #region Private fields
        private bool _ChangedHandlerAdded;
        private bool _CreatedHandlerAdded;
        private bool _DeletedHandlerAdded;
        private bool _RenameHandlerAdded;
        #endregion

        #region constructor
        public FileTextBox()
        {
            AcceptsReturn = true;
            IsReadOnly = true;
            AutoScroll = true;
        }
        #endregion

        #region Properties
        /// <summary>
        /// If true, the FileTextBox will always scroll to the end when updated.
        /// </summary>
        public bool AutoScroll { get; set; }
        #endregion

        #region File Dependency Property
        public string File
        {
            get { return (string)GetValue(FileProperty); }
            set { SetValue(FileProperty, value); }
        }

        // Using a DependencyProperty as the backing store for File.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileProperty =
            DependencyProperty.Register("File", typeof(string), typeof(FileTextBox), new FrameworkPropertyMetadata(OnFilePropertyChanged));

        private static void OnFilePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var ftb = sender as FileTextBox;
            if (ftb == null || args.NewValue == null || string.IsNullOrWhiteSpace(args.NewValue.ToString()))
            {
                return;
            }
            var dir = GetDirectory(ref args);
            if (!string.IsNullOrWhiteSpace(dir) && Directory.Exists(dir))
            {
                ftb.Watcher.Path = Path.GetDirectoryName(args.NewValue.ToString());
                ftb.Watcher.Filter = Path.GetFileName(args.NewValue.ToString());
                ftb.AddEvents();
                ftb.Watcher.EnableRaisingEvents = true;
                ftb.UpdateFile();
            }
            else
            {
                ftb.Text = string.Empty;
            }
        }

        private static string GetDirectory(ref DependencyPropertyChangedEventArgs args)
        {
            try
            {
                return Path.GetDirectoryName(args.NewValue.ToString());
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        private FileSystemWatcher Watcher
        {
            get { return _Watcher ?? (_Watcher = BuildWatcher()); }
        } private FileSystemWatcher _Watcher;

        private FileSystemWatcher BuildWatcher()
        {
            var watcher = new FileSystemWatcher { NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName };
            return watcher;
        }

        public void OnFileDeleted(object sender, FileSystemEventArgs e)
        {
            Dispatcher.Invoke(UpdateFile);
        }

        public void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            Dispatcher.Invoke(UpdateFile);
        }

        public void OnFileCreated(object sender, FileSystemEventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                UpdateFile();
                EnableRaiseEvents();
            });
        }

        public void OnFileRenamed(object sender, RenamedEventArgs e)
        {
            Dispatcher.Invoke(UpdateFile);
        }

        private void EnableRaiseEvents()
        {
            Dispatcher.Invoke(() =>
            {
                if (!Watcher.EnableRaisingEvents)
                    Watcher.EnableRaisingEvents = true;
            });
        }

        private void UpdateFile()
        {
            if (!System.IO.File.Exists(File))
            {

                Text = string.Empty;
                return;
            }
            using (var fs = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var sr = new StreamReader(fs))
                {
                    Text = sr.ReadToEnd();
                    if (AutoScroll)
                        ScrollToEnd();
                }
            }
        }

        private void AddEvents()
        {
            if (!_CreatedHandlerAdded)
            {
                Watcher.Created += OnFileCreated;
                _CreatedHandlerAdded = true;
            }
            if (!_ChangedHandlerAdded)
            {
                Watcher.Changed += OnFileChanged;
                _ChangedHandlerAdded = true;
            }
            if (!_DeletedHandlerAdded)
            {
                Watcher.Deleted += OnFileDeleted;
                _DeletedHandlerAdded = true;
            }
            if (!_RenameHandlerAdded)
            {
                Watcher.Renamed += OnFileRenamed;
                _RenameHandlerAdded = true;
            }
        }
    }
}

As always, feedback is appreciated.