0
votes

what i have tried is?

XAML Code:

  <Grid x:Name="grid" Width="500">
    <RichEditBox Height="32" PointerPressed="RichEditBox_PointerPressed" Background="Transparent" ></RichEditBox>
</Grid>

C# code:

    private void RichEditBox_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
       Debug.WriteLine("Hello");
    }

When I click on RichEditBox,the PointerPressed Evnt won't be fired.I don't know what's the issue was?

In addition to that,when I right click on RichEditBox,it shows the default contextMenu(bold,italic,paste,..),I don't want it .How to stop this also?

1

1 Answers

2
votes

When I click on RichEditBox,the PointerPressed Evnt won't be fired.

PointerRoutedEventArgs has Handled bool property. If any PointerEventHandler marks this Handled as true, the event handler chain doesn’t proceed anymore. The designer of the RichEditBox control had to hook the PointerPressed event for some obvious reason, and in the handler, the Handled was set to true. As a result, the handler you added to the event won’t be invoked.

You can use the AddHandler event handing technique to specify that you want the PointerPressed event handler to be invoked even if the event is marked handled with the third argument set as true, like this:

richEditBox.AddHandler(RichEditBox.PointerReleasedEvent, new PointerEventHandler(RichEditBox_PointerPressed), true);

However, you maybe also note that the document says “Do not routinely ask to rehandle a routed event, because it interferes with the intended design of the Windows Runtime event system for control compositing.”

In addition to the above method, we can also use PointerCaptureLost event which can be invoked when you click the RichEditBox control to replace PointerPressed event.

In addition to that,when I right click on RichEditBox,it shows the default contextMenu(bold,italic,paste,..),I don't want it .How to stop this also?

You can add a new Flyout with the Visibility property set to Collapsed to replace the original Flyout, as a result, no ContextMenu will show when you right click on RichEditBox control. For example:

<RichEditBox x:Name="box" Width="100" Height="32" >
    <RichEditBox.ContextFlyout>
        <Flyout>
            <Flyout.FlyoutPresenterStyle>
                <Style TargetType="FlyoutPresenter">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </Flyout.FlyoutPresenterStyle>
         </Flyout>
    </RichEditBox.ContextFlyout>
</RichEditBox>