Bob G
Bob G

Reputation: 63

WPF - Pushing Enter on Textbox to execute a command

I've seen countless ways to do this - and they all seem more or less the same. But i cannot get this to work at all.

<TextBox x:Name="Assetbox" Text="{UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" VerticalContentAlignment="Center" Grid.Column="2" Grid.ColumnSpan="3">
    <TextBox.InputBindings>
        <KeyBinding Key="Return" Command="{Binding IPfind, Mode=OneWay}"/>
    </TextBox.InputBindings>
</TextBox>

This is an amalgam of several things i saw, and just about the point where i hit the wall and decided to ask for help. Please - what am i screwing up here?

Pushing enter does nothing at all, The IPfind command works just fine if i run it from a button click, but i'd like to avoid that. Any assistance is greatly appreciated.

Upvotes: 0

Views: 221

Answers (1)

Bob G
Bob G

Reputation: 63

Was going about it all wrong, I changed the properties of the text box to:

 <TextBox x:Name="Assetbox" KeyDown="IPfind" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" VerticalContentAlignment="Center" Grid.Column="2" Grid.ColumnSpan="3"/>

And added an if statement to my command to look for key presses

private void IPfind(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {

Upvotes: 1

Related Questions