Any way to make a WPF textblock selectable? Any way to make a WPF textblock selectable? wpf wpf

Any way to make a WPF textblock selectable?


Use a TextBox with these settings instead to make it read only and to look like a TextBlock control.

<TextBox Background="Transparent"         BorderThickness="0"         Text="{Binding Text, Mode=OneWay}"         IsReadOnly="True"         TextWrapping="Wrap" />


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.)

After 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). Most of the functionality around text selection is implemented in System.Windows.Documents.TextEditor system class.

To enable text selection for your control you need to do two things:

  1. Call TextEditor.RegisterCommandHandlers() once to register classevent handlers

  2. Create an instance of TextEditor for each instance of your class and pass the underlying instance of your System.Windows.Documents.ITextContainer to it

There's also a requirement that your control's Focusable property is set to True.

This is it! Sounds easy, but unfortunately TextEditor class is marked as internal. So I had to write a reflection wrapper around it:

class TextEditorWrapper{    private static readonly Type TextEditorType = Type.GetType("System.Windows.Documents.TextEditor, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");    private static readonly PropertyInfo IsReadOnlyProp = TextEditorType.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);    private static readonly PropertyInfo TextViewProp = TextEditorType.GetProperty("TextView", BindingFlags.Instance | BindingFlags.NonPublic);    private static readonly MethodInfo RegisterMethod = TextEditorType.GetMethod("RegisterCommandHandlers",         BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(Type), typeof(bool), typeof(bool), typeof(bool) }, null);    private static readonly Type TextContainerType = Type.GetType("System.Windows.Documents.ITextContainer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");    private static readonly PropertyInfo TextContainerTextViewProp = TextContainerType.GetProperty("TextView");    private static readonly PropertyInfo TextContainerProp = typeof(TextBlock).GetProperty("TextContainer", BindingFlags.Instance | BindingFlags.NonPublic);    public static void RegisterCommandHandlers(Type controlType, bool acceptsRichContent, bool readOnly, bool registerEventListeners)    {        RegisterMethod.Invoke(null, new object[] { controlType, acceptsRichContent, readOnly, registerEventListeners });    }    public static TextEditorWrapper CreateFor(TextBlock tb)    {        var textContainer = TextContainerProp.GetValue(tb);        var editor = new TextEditorWrapper(textContainer, tb, false);        IsReadOnlyProp.SetValue(editor._editor, true);        TextViewProp.SetValue(editor._editor, TextContainerTextViewProp.GetValue(textContainer));        return editor;    }    private readonly object _editor;    public TextEditorWrapper(object textContainer, FrameworkElement uiScope, bool isUndoEnabled)    {        _editor = Activator.CreateInstance(TextEditorType, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance,             null, new[] { textContainer, uiScope, isUndoEnabled }, null);    }}

I also created a SelectableTextBlock derived from TextBlock that takes the steps noted above:

public class SelectableTextBlock : TextBlock{    static SelectableTextBlock()    {        FocusableProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata(true));        TextEditorWrapper.RegisterCommandHandlers(typeof(SelectableTextBlock), true, true, true);        // remove the focus rectangle around the control        FocusVisualStyleProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata((object)null));    }    private readonly TextEditorWrapper _editor;    public SelectableTextBlock()    {        _editor = TextEditorWrapper.CreateFor(this);    }}

Another option would be to create an attached property for TextBlock to enable text selection on demand. In this case, to disable the selection again, one needs to detach a TextEditor by using the reflection equivalent of this code:

_editor.TextContainer.TextView = null;_editor.OnDetach();_editor = null;


I have been unable to find any example of really answering the question. All the answers used a Textbox or RichTextbox. I needed a solution that allowed me to use a TextBlock, and this is the solution I created.

I believe the correct way to do this is to extend the TextBlock class. This is the code I used to extend the TextBlock class to allow me to select the text and copy it to clipboard. "sdo" is the namespace reference I used in the WPF.

WPF Using Extended Class:

xmlns:sdo="clr-namespace:iFaceCaseMain"<sdo:TextBlockMoo x:Name="txtResults" Background="Black" Margin="5,5,5,5"       Foreground="GreenYellow" FontSize="14" FontFamily="Courier New"></TextBlockMoo>

Code Behind for Extended Class:

public partial class TextBlockMoo : TextBlock {    TextPointer StartSelectPosition;    TextPointer EndSelectPosition;    public String SelectedText = "";    public delegate void TextSelectedHandler(string SelectedText);    public event TextSelectedHandler TextSelected;    protected override void OnMouseDown(MouseButtonEventArgs e)    {        base.OnMouseDown(e);        Point mouseDownPoint = e.GetPosition(this);        StartSelectPosition = this.GetPositionFromPoint(mouseDownPoint, true);                }    protected override void OnMouseUp(MouseButtonEventArgs e)    {        base.OnMouseUp(e);        Point mouseUpPoint = e.GetPosition(this);        EndSelectPosition = this.GetPositionFromPoint(mouseUpPoint, true);        TextRange otr = new TextRange(this.ContentStart, this.ContentEnd);        otr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.GreenYellow));        TextRange ntr = new TextRange(StartSelectPosition, EndSelectPosition);        ntr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));        SelectedText = ntr.Text;        if (!(TextSelected == null))        {            TextSelected(SelectedText);        }    }}

Example Window Code:

    public ucExample(IInstanceHost host, ref String WindowTitle, String ApplicationID, String Parameters)    {        InitializeComponent();        /*Used to add selected text to clipboard*/        this.txtResults.TextSelected += txtResults_TextSelected;    }    void txtResults_TextSelected(string SelectedText)    {        Clipboard.SetText(SelectedText);    }