按Enter键时,根据其他单元格更新Datagrid单元格

时间:2012-07-01 12:51:18

标签: c# wpf xaml mvvm

我使用Galasoft的EventToCommand作为代码,在用户插入价格和数量后更新行总计单元格。 请在插入价格,数量并按Enter键时,帮我找到合适的方法来更改行总计。我尝试了InputBindings,但遗憾的是没有用。

以下是我视图中的Datagrid XAML:

<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>

以下是ViewModel中的CurrentCellChangedCommand excute方法(SelectedItem是一个EF实体):

private void RecalculateLineTotal()
{
    if (SelectedItem != null)
    SelectedItem.Line_Total = SelectedItem.Quantity * SelectedItem.UnitePrice;
}

提前致谢

1 个答案:

答案 0 :(得分:0)

一段时间后它开始正常工作。我认为这是一些额外的阻止那些Key stroks的工作。