WPF - Key.Up, Key.Down commands sent from Textbox to a MVVM Command binding

Post date: Sep 5, 2012 7:26:01 AM

I want to use the keyboard for shortcuts while I am typing in a TextBox in WPF.

When the Text property of the TextBox is changed, the ListBox will show a filtered list. Using the keyboard you can then move the selector up or down.

Part of my semi-auto-completion ListBox. I developed it for my data producer project. To see how it all hooked up, refer to the source code until I have a moment to describe how it is set up.

http://code.google.com/p/sql-server-data-producer/source/browse/

<TextBox Grid.Row="0" Text="{Binding Model.SearchCriteria, UpdateSourceTrigger=PropertyChanged}" > <TextBox.InputBindings> <KeyBinding Key="Up" Command="{Binding MoveUpWithTheSelectorCommand}" /> <KeyBinding Key="Down" Command="{Binding MoveDownWithTheSelectorCommand}" /> </TextBox.InputBindings></TextBox>

The commands in the ViewModel:

MoveUpWithTheSelectorCommand = new DelegateCommand(() => { Model.TablesView.MoveCurrentToPrevious(); Model.SelectedTable = Model.TablesView.CurrentItem as TableEntity; }); MoveDownWithTheSelectorCommand = new DelegateCommand(() => { Model.TablesView.MoveCurrentToNext(); Model.SelectedTable = Model.TablesView.CurrentItem as TableEntity; });