Private static readonly PropertyInfo TextViewProp = TextEditorType.GetProperty("TextView", BindingFlags.Instance | BindingFlags.NonPublic)
Private static readonly PropertyInfo IsReadOnlyProp = TextEditorType.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic) Private static readonly Type TextEditorType = Type.GetType(", PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") So I had to write a reflection wrapper around it: class TextEditorWrapper This is it! Sounds easy, but unfortunately TextEditor class is marked as internal. There's also a requirement that your control's Focusable property is set to True. To enable text selection for your control you need to do two things:Ĭall TextEditor.RegisterCommandHandlers() once to register classĬreate an instance of TextEditor for each instance of your class and pass the underlying instance of your to it Most of the functionality around text selection is implemented in system class.
All the answers here are just using a TextBox or trying to implement text selection manually, which leads to poor performance or non-native behaviour (blinking caret in TextBox, no keyboard support in manual implementations etc.)Īfter hours of digging around and reading the WPF source code, I instead discovered a way of enabling the native WPF text selection for TextBlock controls (or really any other controls).