uwpricheditbox

Pointer Released On RichEditBox UWP


I would like to use the PointerReleased event in a RichEditBox but it does not work.

Xaml:

<RichEditBox x:Name="RebText" PointerReleased="RebText_PointerReleased"/>

Xaml.cs:

private void RebText_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    throw new NotImplementedException();
}

Thanks in advance!


Solution

  • Reson it doesn't work:

    PointerRoutedEventArgs has a Handled bool Property. If any PointerEventHandler marks this Handled as true, the event handler chain doesn't proceed anymore.

    Now, the designer of the RichEditBox control had to hook the PointerReleased event for some obvious reason, and in the handler, he must have marked the Handled to true. As a result, the handler you added to the event won't be invoked.

    This also happens in other controls. An example is: you can't hook the PointerWheelChanged event of a ScrollViewer by just simply adding an EventHandler to it, like :

    myScrollViewer.PointerWheelChanged += Some_Handler;
    

    The handler will be added, but it won't be invoked for the same reason.

    Solution:

    All UIElements have an AddHandler method. This method also adds EventHandlers to events, just like the += operator does, but the benefit is, you can:

    have the provided handler be invoked even if the event is handled elsewhere

    So, what you should do is, something like:

    RebText.AddHandler(RichEditBox.PointerReleasedEvent, new PointerEventHandler(RebText_PointerReleased), true);
    

    and then define RebText_PointerReleased:

    private void RebText_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        // your event handling code here
    }
    

    The third argument is where you specified that you want this handler to be invoked even if the event is marked handled.

    Note:, it's not a good practice to handle an already handled event, because, as the doc 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.