简体   繁体   中英

Update Datagrid cell according to other cells when hit Enter

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM