1

I have a TextBox with an 'OK' button below it. The 'OK' button runs the method in my RunPanel static class like this: RunPanel.RunOK();.

I would like pressing enter in the TextBox to also execute the method RunPanel.RunOK().

How could I do this in XAML?

The answers I have come across seem confusing, so I was wondering if someone could help me out.

1
  • what is RunPanel? where that is written ? in code behind? or some model? you're following any patterns(mvvm,prism...) ? you're writing an application or control? Commented Jan 2, 2017 at 10:48

3 Answers 3

3
private void textBox_KeyUp(object sender, KeyEventArgs e)
    {
       if(e.Key == Key.Return)
        {
            MessageBox.Show("Enter key pressed, put your method in here!");
        }
    }

This does not help you execute the method you specified in XAML, however having the textbox subscribe to a KeyUp event, and then performing a check on which key was entered solves your issue. You can put RunPanel.RunOK() inside the if statement in this case, and it will fire IF the enter key was pressed inside the textbox.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry. I have edited the answer and tried to provide some clarification on why it solves the problem.
Perfect. Thank you. May your future at this site be bright and may you contribute and get many reputation points and more and more access, and leverage the many bounties the community provides here, and also check out the review link :-) Remember, there are badges and hats, and when you pay a lot of dues, not only due you get respect of the community but access to some neat features :-)
I was aware of this solution, however my purpose is to do this XAML-only, as I think it's cleaner, but alas I may as well use this.
0

KeyBinding would be probably your best shot.

For instance:

<Window>
   <Window.InputBindings>
    <KeyBinding Command="{Binding SomeCommand, RelativeSource={RelativeSource Self}}" Modifiers="Control" Key="F5"></KeyBinding>
   </Window.InputBindings>
 </Window>

4 Comments

I'm just curious: does this bind to the 'F5' key or the 'enter' key?
Yep, I got that far - however I was wondering how to replace 'SomeCommand' with RunPanel.RunOK() and have it work?
@zx485 this is for CTRL + F5, serving as a general example. I used Modifiers there to show how to use key modifiers
@AT does RunPanel.RunOK() returns any value? If so, wrap it into a Getter, callind the method: RunOKReturnType SomeCommand { get { return RunPanel.RunOk() } } Otherwise you want to search how to bind to a method, which is a little more complciated but also possible.
0

How could I do this in XAML?

XAML is a markup language and you cannot really call methods in it. You could bind to a method using an ObjectDataProvider as described here: http://www.devcurry.com/2011/03/wpf-4-using-objectdataprovider-for.html

But if you want to do something when a button is clicked you should bind to a command of a view model and call your static method in the Execute method of this command.

Trying to everything or as much as possible in XAML just because you can and just for the sake of eliminating code is generally considered as an anti-pattern.

MVVM is not about eliminating code, it's mainly about separation of concerns and C# is a much more expressive and more concise language than XAML. So you should implement the behaviour of your application using a programming such as C# language and use XAML to define the visual presentation and the layout of your UI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.