I want to write a Trigger on a WPF DataGridRow as follows-
When user makes any change in any cell of that row, the last cell of same row should be updated by some specific value.
Please help....
I want to write a Trigger on a WPF DataGridRow as follows-
When user makes any change in any cell of that row, the last cell of same row should be updated by some specific value.
Please help....
I'm not sure if this can be done with a trigger.
For an MVVM-based solution you could change your entity to implement IEditableObject
. When you commit a cell change in the DataGrid (e.g. tabbing off or pressing Enter), the entity's IEditableObject.EndEdit()
method will get called. You could then update your last cell's bound property in there.
(For some reason EndEdit()
is called twice, which appears to be a quirk/bug when using IEditableObject with DataGrids. The problem is mentioned here).
If you want more control of if/when IEditableObject.EndEdit()
gets called, you can use the DataGrid RowEditEnding and CellEditEnding events. E.g. the following RowEditEnding
event handler code will ensure that the entity's EndEdit() method gets called only when you commit an entire row (rather than after each cell commit):-
private void RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (_isCommittingEdit || e.EditAction == DataGridEditAction.Cancel)
{
return;
}
if (e.EditAction == DataGridEditAction.Commit)
{
_isCommittingEdit = true;
try
{
grid1.CommitEdit(DataGridEditingUnit.Row, true);
}
finally
{
_isCommittingEdit = false;
}
}
}
I've found that calling DataGrid.CommitEdit()
causes the same event to fire again, resulting in a stack overflow, hence the _isCommittingEdit
variable to stop this from happening. (I would be interested to know if it's another quirk or something that I'm doing wrong!)
I solved my problem using the solution provided by Brian Hinchey at the following link- How to get property change notifications with EF 4.x DbContext generator