I’m using Galasoft’s EventToCommand as code below to update Line Total cell after user inserts price and quantity.
Please, help me find the appropriate way to make the Line Total changes when I insert the price, quantity and hit Enter. I tried InputBindings, but unfortunately didn’t work.
Here is the Datagrid XAML from my View:
<DataGrid IsReadOnly="False" x:Name="_StockCardItems" ItemsSource="{Binding InvoiceDetailsList, Mode=TwoWay}" SelectedItem="{Binding SelectedItem}" CanUserDeleteRows="True" CanUserAddRows="False">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CurrentCellChanged">
<gs:EventToCommand PassEventArgsToCommand="True" Command="{Binding CurrentCellChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.InputBindings>
<KeyBinding Key="Enter" Command="{Binding CurrentCellChangedCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Désignation" Width="400" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Margin="10,2" HorizontalAlignment="Left" Text="{Binding Path=Items.Designation}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Quantité" Width="150" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBox Margin="10,0" HorizontalAlignment="Center" GotKeyboardFocus="TextBox_GotKeyboardFocus" GotMouseCapture="TextBox_GotMouseCapture" IsReadOnly="False" Text="{Binding Path=Quantity, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource RemoveDoubleZero}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="PU" Width="150" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBox Margin="10,0" HorizontalAlignment="Center" IsReadOnly="False" GotKeyboardFocus="TextBox_GotKeyboardFocus" GotMouseCapture="TextBox_GotMouseCapture" Text="{Binding Path=UnitePrice, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource RemoveDoubleZero}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Montant HT" Width="150" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Margin="10,2" HorizontalAlignment="Right" Text="{Binding Path=Line_Total,Mode=TwoWay,ValidatesOnExceptions=True,ValidatesOnDataErrors=True, StringFormat=n, ConverterCulture=fr-FR}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Here is the CurrentCellChangedCommand excute method in the ViewModel (SelectedItem is an EF Entity):
private void RecalculateLineTotal()
{
if (SelectedItem != null)
SelectedItem.Line_Total = SelectedItem.Quantity * SelectedItem.UnitePrice;
}
Thanks in advance
It started to work normally after sometime. I think that was something extra prevented those Key stroks from working.