TreeListView - How to capture an enter key press on a textbox in the data template of a cell

Grids for WPF Forum

Posted 2 months ago by Patrick Milinazzo - Woodward, Inc.
Version: 24.1.4
Avatar

Using the TreeListView I want to react to the user pressing the enter key on a textbox that I have added to the cell template of a column. I am listening to the PreviewTextinput event on the text box. It seems the tree item is swallowing up the enter key as I get the other keys that are pressed.  I cannot use the EditableContentControl in the column because along with the textbox I have up/down buttons and other controls. I also cannot use a different event because I want to use the e.Text from the event args.

<TextBox x:Name="_inputTextBox" MinHeight="19"
    PreviewTextInput="_inputTextBox_PreviewTextInput"
    TextAlignment="Right" VerticalAlignment="Center"
    Visibility="{Binding ReadOnly, Converter={StaticResource BooleanTrueToVisibilityCollapsedConverter}}"
    Padding="16,1,1,1" TextOptions.TextRenderingMode="ClearType" TextOptions.TextFormattingMode="Display"
    UndoLimit="99" Style="{Binding TextBoxStyle, ElementName=_root}"/>
 
 
void _inputTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) {
    //Process the input so far when the Enter key is pressed.
    if (e.Text == "\r") {
        ValidateInputTextBox(e);
        return;
    }
}

Comments (1)

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Patrick,

I think that what is happening here is that the TreeListBox (base class of TreeListView) has a protected virtual method named ProcessKeyDown that handles various keys for navigation and other actions.  That method handles the Enter key to execute the default action for the active tree node.  KeyDown events occur prior to text composition events, so our processing is probably swallowing up the key before it is used for text input.

What you could do to work around this is override our ProcessKeyDown method and if the key is Enter and focus is in a TextBox, don't call our base method.


Actipro Software Support

The latest build of this product (v24.1.5) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.