0

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

Ankit Goel
  • 353
  • 1
  • 4
  • 13

2 Answers2

0

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

Community
  • 1
  • 1
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • What i feel is that, here we don't need to use grid1.CommitEdit(DataGridEditingUnit.Row, true); I achieved the same using the following code- private void DgTeam_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (_context.Entry((Team)e.Row.Item).State == EntityState.Modified) { ((Team)e.Row.Item).Mode = "E"; } } but i am trying to find out whetehr this can be done using xaml – Ankit Goel Aug 07 '14 at 05:22
  • I think this is a little too complicated to be achieved with a trigger. Sometimes you have to resort to a code-behind event handler! If you are using MVVM and are concerned about putting business logic in code-behind, it could call a method on the VM to set the last column value (assuming you are using "view-first", i.e. the view has a reference to the VM). – Andrew Stephens Aug 11 '14 at 08:52
0

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

Community
  • 1
  • 1
Ankit Goel
  • 353
  • 1
  • 4
  • 13