Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeViewItem with TextBox in WPF: type special characters

I need to edit some hierarchical structure and I use TreeView with TextBoxes

Short example

<TreeView>
    <TreeView.Items>
        <TreeViewItem Header="Level 0">
            <!-- Level 1-->
            <TextBox Margin="5"
                     BorderThickness="1" BorderBrush="Black" />
        </TreeViewItem>
    </TreeView.Items>
</TreeView>

When I type in TextBox, +, -, letters and digits work fine, arrows work but when I press -, Level 0 item collapses and when I type *, nothing happens

How should I handle - and * to see them in TextBox as expected?

Edit:

- works if typed as Key.OemMinus but not from numeric keyboard as Key.Subtract

* works if typed as Shift+Key.D8 but not from numeric keyboard as Key.Multiply

like image 848
ASh Avatar asked Aug 28 '15 07:08

ASh


People also ask

How do you escape from XAML?

The escape sequence ({}) is used so that an open brace ({) can be used as a literal character in XAML. XAML readers typically use the open brace ({) to denote the entry point of a markup extension; however, they first check the next character to determine whether it is a closing brace (}).


2 Answers

finally solved the problem with Key.Subtract

I added handler to PreviewKeyDown on TextBox

<TextBox Margin="5" BorderThickness="1" BorderBrush="Black" 
         PreviewKeyDown="TextBoxPreviewKeyDown"
/>

on receiving Key.Subtract, KeyDown is marked as handled and then i manually raise TextInput event as explained in this answer (How can I programmatically generate keypress events in C#? )

private void TextBoxPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Subtract)
    {
        e.Handled = true;

        var text = "-";
        var target = Keyboard.FocusedElement;
        var routedEvent = TextCompositionManager.TextInputEvent;

        target.RaiseEvent(
            new TextCompositionEventArgs
                (
                     InputManager.Current.PrimaryKeyboardDevice,
                    new TextComposition(InputManager.Current, target, text)
                )
                {
                    RoutedEvent = routedEvent
                });
    }
}
like image 162
ASh Avatar answered Oct 14 '22 06:10

ASh


I can suggest a keydown event for the textboxes that you have.

<TextBox Margin="5" KeyDown="TextBox_KeyDown"
                     BorderThickness="1" BorderBrush="Black" />


 private void TextBox_KeyDown(object sender, KeyEventArgs e)
 {
    TextBox txt = sender as TextBox;
    if(e.Key == Key.Subtract)
    {
        txt.Text += "-";
        txt.SelectionStart = txt.Text.Length;
        txt.SelectionLength = 0;
        e.Handled = true;
    }
    else if (e.Key == Key.Multiply)
    {
        txt.Text += "*";
        txt.SelectionStart = txt.Text.Length;
        txt.SelectionLength = 0;
        e.Handled = true;
    }
}

It's not a good solution but it works. If you have any other "problem" keys, you can add an if to the event.

SelectionStart and SelectionLength are for positioning cursor at the end of textbox. And e.Handled = true; does prevent the default behaviour.

like image 5
sertsedat Avatar answered Oct 14 '22 05:10

sertsedat