Search code examples
c#wpfxamlrichtextboxflowdocument

How to prevent the highlighting of InlineUIContainer on mouse click?


Consider the following FlowDocument inside a WPF RichTextBox:

<RichTextBox Width="150" FontSize="30">
  <FlowDocument>
    <Paragraph>
      <Run Text="abc"/>
      <InlineUIContainer>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="4" Width="30" Height="20"/>
      </InlineUIContainer>
      <Run Text="def"/>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

When I click on the Border it becomes highlighted in blue:

highlight

How can I prevent that? I would like it to position the caret instead, like this:

enter image description here

I have already tried the following properties without success:

  • IsHitTestVisible = false
  • IsEnabled = false
  • IsManipulationEnabled = false
  • Focusable = false
  • IsFocused = false

Solution

  • use this xaml:

     <RichTextBox x:Name="rchText" Width="150" FontSize="30">
          <FlowDocument MouseDown="rch_MouseDown">
             <Paragraph>
                  <Run Text="abc"/>
                  <InlineUIContainer>
                 <Border x:Name="brdText" BorderBrush="Black" BorderThickness="1" CornerRadius="4" Width="30" Height="20"/>
                 </InlineUIContainer>
                 <Run Text="def"/>
            </Paragraph>
         </FlowDocument>
    </RichTextBox>
    

    then:

    private void rch_MouseDown(object sender, MouseButtonEventArgs e)
    {
       var mousePoint = Mouse.GetPosition(rchText);
       var borderPos = brdText.TranslatePoint(new Point(0, 0), rchText);
       if(mousePoint.X > borderPos.X && mousePoint.X < (borderPos.X + brdText.ActualWidth) && mousePoint.Y > borderPos.Y && mousePoint.Y < (borderPos.Y + brdText.ActualWidth))
       {
         e.Handled = true;
       }
    }