Pressing Enter on TextBox in Silverlight
Asked Answered
U

5

10

I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".

It just feels like there is a better way to do this. So the question is, is there?

Unmannerly answered 23/11, 2008 at 20:33 Comment(0)
B
12

I thinks that's the way to catch Key.Enter.

Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.

If you only care about catching Key.Enter for a single control then your approach is correct.

You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").

Bandstand answered 24/11, 2008 at 2:48 Comment(2)
To use Event Bubbling is a great idea! my book about SL: Bubbling events are events that travel up the containment hierarchy. For example, MouseLeftButtonDown is a bubbling event. It’s raised first by the element that is clicked. Next, it’s raised by that element’s parent, and then by that element’s parent, and so on, until Silverlight reaches the top of the element tree.Nanoid
Could you elabaorate on "more readable if you use the KeyDown event instead of the KeyUp event"?Kossuth
M
6

Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.

Metallic answered 23/11, 2008 at 21:6 Comment(1)
I would have never have thought of that but it works perfectly. I had the TextBox in a UserControl and just attached to the KeyUp event of the UserControl. This was after trying to get the UserControl to create a separate custom event.Gusto
M
5

This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:

    private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter) SomeButton_Click(this, null);

    }

    private void SomeButton_Click(object sender, RoutedEventArgs e)
    {
        ICommand cmd = SomeButton.Command;
        if (cmd.CanExecute(null))
        {
            cmd.Execute(null);
        }
    }
Mauritamauritania answered 12/7, 2011 at 10:6 Comment(0)
M
1

I use the following implementation when using the command pattern:

private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty); 
        if (b != null)
            b.UpdateSource();

        ICommand cmd = SomeButton.Command;
        if (cmd.CanExecute(null))
            cmd.Execute(null);
    }
}

When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.

Of course you can catch the event on a higher level than the textbox.

Mundy answered 5/6, 2012 at 13:19 Comment(0)
A
0

Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.

And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.

Ariannearianrhod answered 16/3, 2011 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.