Just Code‎ > ‎

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

posted Sep 5, 2012, 12:26 AM by Peter Henell   [ updated Sep 5, 2012, 12:28 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.

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

The commands in the ViewModel:
MoveUpWithTheSelectorCommand = new DelegateCommand(() =>
                    Model.SelectedTable = Model.TablesView.CurrentItem as TableEntity;
            MoveDownWithTheSelectorCommand = new DelegateCommand(() =>
                Model.SelectedTable = Model.TablesView.CurrentItem as TableEntity;