How Can I make my WPF RichtextBox Contents Highlightable while in Disabled Mode?

Mesh Ka 345 Reputation points
2023-08-12T11:59:39.8866667+00:00

I have 2 WPF Rich textbox side by side, when the user is writing in one of the Rich textboxes the other one is disabled, Sometimes my Users Highlight Text in one of the Rich Textboxes and move to the other one, the action of moving to the other Rich textbox will disable the current one with highlighted text (that is how it was meant to be), but my problem is that the highlighted Text will not be visible because Rich textbox is disabled.

How Can I make the disabled WPF Rich Textbox Highlights visible?

I tried Styling and templating but with no idea how to do it.

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Brian Zarb 1,685 Reputation points
    2023-08-12T12:44:52.5033333+00:00

    Hey Mesh, this cant be achieved in the disabled state, however, there is a workaround that gets you to the same result

    We need to Handle the PreviewMouseDown, PreviewMouseUp, and PreviewMouseMove events: This way, you can disable text editing but allow text selection. By handling these events, you can determine whether the user is trying to select text and allow that action, while preventing others.

    Styling: Apply the necessary styles to make the RichTextBox appear "disabled" (for example, grayed out), while keeping the highlights visible.

    I created a quick example to show you how this would look like:

    <RichTextBox x:Name="rtb1"
                 PreviewMouseDown="RichTextBox_PreviewMouseDown"
                 PreviewMouseUp="RichTextBox_PreviewMouseUp"
                 PreviewMouseMove="RichTextBox_PreviewMouseMove">
        <RichTextBox.Resources>
            <Style TargetType="RichTextBox">
                <Setter Property="Background" Value="LightGray"/>
            </Style>
        </RichTextBox.Resources>
    </RichTextBox>
    <RichTextBox x:Name="rtb2" />
    

    C# code-behind:

    private bool isSelectingText = false;
    private void RichTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        var rtb = sender as RichTextBox;
        if (rtb != null && rtb.Selection.IsEmpty)
        {
            e.Handled = true;
        }
        else
        {
            isSelectingText = true;
        }
    }
    private void RichTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        isSelectingText = false;
    }
    private void RichTextBox_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (!isSelectingText)
        {
            e.Handled = true;
        }
    }
    

    with this method, the RichTextBox will look disabled due to the LightGray background, but the text selection is still allowed.

    When a user tries to click without dragging (i.e., possibly for editing), the PreviewMouseDown event handler prevents the action if there's no selection. The PreviewMouseMove event handler ensures text can be highlighted by allowing the mouse move events only if the user is in the process of selecting text.

    If you found this helpful, please mark it as the answer and consider following! Thank you


1 additional answer

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2023-08-12T13:09:11.7966667+00:00

    You can set : IsReadOnly="True"

    It won't be editable but highlightable

    RichTextBox_ReadOnly

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.