I'm working on a WPF MVVM project. A DataGrid shows the records found by a service. Each row has a button which executes an action with the record chosen.
public class ReferenceDossier
{
}
XAML
<GroupBox>
<Grid>
<StackPanel>
<TextBox>
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SearchCommand}"/>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
</Grid>
</GroupBox>
<GroupBox>
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding ReferenceDossiers}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Command}"
CommandParameter="{Binding}">>
<iconPacks:PackIconFontAwesome Kind="PlusCircle" Foreground="#FF94bf00"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</GroupBox>
When I make a search with the keyword in the TextBox
and tap Enter, it performs a calculation and shows the results in DataGrid
.
What I want to code:
If the user press ENTER again (after records shown on DataGrid
) and the user hasn't selected yet a row, the Command
of the Button
of the first row must be invoked, not important how many records in the grid, the first one must be chosen.
If the user selects a row in DataGrid
and press ENTER, the Command
of the Button
of that row is invoked.
If zero record, do nothing.
The goal is to reduce the time of chose on GUI.
But I can't find a way to do that by MVVM approach.
With some tries, when I press ENTER after tapping the keyword, TextBox
is always focused. I press ENTER again, it reperforms the calculation.