Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 19-Sep-19 2:01am
v2
Comments
Maciej Los 19-Sep-19 8:00am    
Seems you forgot to set Mode="TwoWay" for Binding. See: Binding.Mode Property (System.Windows.Data) | Microsoft Docs[^]
Patrick Skelton 20-Sep-19 2:47am    
The solution I found below seems to work without this, but I have to admit I wasn't aware it was one-way by default, so that would probably have crawled out and bit me at some point. Thanks for the suggestion.
Maciej Los 20-Sep-19 2:51am    
You're very welcome.

1 solution

It seems that what I need is this:

<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity,UpdateSourceTrigger=LostFocus}"/>


As detailed in the following CodeProject question, which I have only just found. :-|

https://www.codeproject.com/Questions/406102/WPF-DataGrid-immediate-update-on-Cell-Change
 
Share this answer
 
Comments
Maciej Los 19-Sep-19 8:34am    
Great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900