Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do Wpf TextBoxes only mark some Events as Handled #10138

Open
PxC-MKias opened this issue Dec 3, 2024 · 4 comments
Open

Why do Wpf TextBoxes only mark some Events as Handled #10138

PxC-MKias opened this issue Dec 3, 2024 · 4 comments
Labels
Question General question, not a problem in source code or documentation (yet)

Comments

@PxC-MKias
Copy link

PxC-MKias commented Dec 3, 2024

Description

I work on some Interaction Detection and use the fact that Controls mark RoutedEvents as Handled when they handle them.
Unfortunately, TextBoxes only mark TextCompositionEventArgs in the OnTextInput method and ExecutedRoutedEventArgs in the OnSpace as Handled (see TextEditorTyping.cs#L386). All other Events (Delete, Key Events, ...) that are also implemented in TextEditorTyping are not marked as handled. Is there a Reason for that? It is a problem for my usecase.

Reproduction Steps

Register a ClassHandler for handled Events and debug what Controls handle the events.

KeyEventHandler keyEventHandler = /* please ask for the implementation if its necessary */
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyDownEvent, keyEventHandler, true);
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, keyEventHandler, true);
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyUpEvent, keyEventHandler, true);
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyUpEvent, keyEventHandler, true);

Expected behavior

TextBoxes handle all Events that change its text.

Actual behavior

TextBoxes only mark TextCompositionEventArgs in the OnTestInput method and ExecutedRoutedEventArgs in the OnSpace as Handled.
All common KeyEvents do not mark events as Handled, as well as all other event handling methods OnBackspace, OnDelete, OnToggleInsert, ...

TextEditorTyping.cs#L386

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

Not configuration specific as far as I know, but the target framework is net80-windows

Other information

No response

@Symbai
Copy link
Contributor

Symbai commented Dec 3, 2024

Is there a Reason for that?

Because for example if you handle PreviewKeyDownEvent, the KeyDownEvent is not raised anymore. But also if a parent mousewheel event is handled, subscribers for a child control mousewheel event are not notified as well. Therefore it would be ridiculous if the framework marks all events as handled.

When to mark such an event as handled is described here

@miloush
Copy link
Contributor

miloush commented Dec 3, 2024

That is a good find, Symbai, for reference.

I think the answer is a bit simpler though: The TextBox does not actually handle these events for typing. Typing happens as a result of text composition and that's what gets handled.

@PxC-MKias
Copy link
Author

Because for example if you handle PreviewKeyDownEvent, the KeyDownEvent is not raised anymore. But also if a parent mousewheel event is handled, subscribers for a child control mousewheel event are not notified as well. Therefore it would be ridiculous if the framework marks all events as handled.

When to mark such an event as handled is described here

Thank you for your response. I understand that the PreviewKeyDownEvent is not marked handled. I would expect that KeyDownEvent is handled, but you could argue that it is enough that TextCompositionEventArgs is marked handled.
As the source you shared states, elements should be marked as handled when setting properties that affect visual representation. In my opinion this includes every event that modifies the text of a TextBox and therefore changes the visual representation.

Significant responses include setting focus, modifying public state, setting properties that affect visual representation, raising new events, and completely handling an event.

It also seems inconsistent that the OnSpace method marks the event as handled but the OnDelete not.

@PxC-MKias
Copy link
Author

PxC-MKias commented Dec 3, 2024

That is a good find, Symbai, for reference.

I think the answer is a bit simpler though: The TextBox does not actually handle these events for typing. Typing happens as a result of text composition and that's what gets handled.

Yes, text composition seems understandable for normal characters. But what is with the commands like Delete/Space? They are not marked handled consistently

@himgoyalmicro himgoyalmicro added the Question General question, not a problem in source code or documentation (yet) label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question General question, not a problem in source code or documentation (yet)
Projects
None yet
Development

No branches or pull requests

4 participants