D
D
Drottarutarnum2019-05-30 15:27:44
WPF
Drottarutarnum, 2019-05-30 15:27:44

How to execute command on KeyDown event for TextBox?

How to execute command on KeyDown event in TextBox?
I'm using WPF

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2019-05-31
@Drottarutarnum

You can use a converter i:InvokeCommandActionfrom System.Windows.Interactivity.
1) Add an assembly System.Windows.Interactivityby adding a link (it's in the list of extensions).
2) Add the specified assembly to the xaml:
3) Use i:EventTriggerand i:InvokeCommandAction:

<TextBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown">
            <i:InvokeCommandAction Command="{Binding KeyDownCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

But if you want to call only view processing there, for example, to disable certain keys (that is, you don’t need data from the business logic layer), then it makes sense to do the processing in the usual way - in code-behind (xaml.cs).

P
Peter, 2019-05-30
@petermzg

And what prevents you from writing a function in the code-behind that will be attached to the control from which you execute the command. code-behind and XAML is the presentation layer. And via DataContext get your ViewModel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question