I have a class of cost-items that automatically returns the total value of the item, like this:
public class EstimateCostItem
{
public double UnitCost
{
get => _unitCost;
set => _unitCost = value;
}
public double Quantity
{
get => _quantity;
set => _quantity = value;
}
public double Subtotal => _unitCost * _quantity;
private double _quantity = 0.0;
private double _unitCost = 0.0;
}
In my model layer, I have a collection of these, like this:
public List<EstimateCostItem> CostItems { get; } = new List<EstimateCostItem>();
I display these items something like this:
<DataGrid x:Name="DataGridCostItems"
ItemsSource="{Binding CostItems}"
SelectionMode="Single"
</DataGrid>
I also have some code that is designed to notify the view-model (and hence the view) of changes when appropriate. This code seems to be working fine.
The problem I have is that, when the user edits either the
UnitCost
or the
Quantity
, the
Subtotal
does not update until the user leaves the row currently being edited. This is confirmed by a breakpoint in the
set
methods above; these breakpoints are not hit until the user leaves the whole row.
I don't care one way or the other if the model is updated when the user leaves an individual cell but it looks confusing to the user that the subtotal does not update until the user leaves the whole row.
Is there any way to change this behaviour?
What I have tried:
I have tried following the example here:
WPF DataGrid Committing Cell Changes.
But I always get null for the following code in the
CellEditEnding
event:
DataRowView row = e.Row.Item as DataRowView;
The type I get is
DataGridRow
. This doesn't have an
EndEdit()
method and there seems to be no way to get a
DataRowView
from a
DataGridRow
.
I think the problem is because I am not binding to a
DataTable
but I'm still struggling to see a way round this.