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