0

I have a UserControl, which is basically a wrapper for a GridView that needs to send a message every time a cell content (of the GridView) is changed. Usually, that could be solved like this:

private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        var editingTextBox = e.EditingElement as TextBox;
        doSomething(editingTextBox.Text);
    }

The problem is that I don't know the type of EditingElement (which comes as a FrameworkElement) so I cannot do the conversion. And in that moment, the currentCell.SelectedValue is still the original value. I also don't have control over the model (where I could implement INotifyPropertyChanged and use it to catch the changes).

Is there some simple way I am missing? Or how would you go about implementing this? Thank you for any suggestions.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Jan Kratochvil
  • 2,307
  • 1
  • 21
  • 39

2 Answers2

1

Wrapping your model within a CollectionView and use this for the binding.

myCollectionView = (CollectionView)
    CollectionViewSource.GetDefaultView(rootElem.DataContext);

This will provide you a INotifyPropertyChanged interface.

Update Sorry my first answer was somewhat misleading.

If you're not able to change the model, you should create a view model. The view model, implementing INotifyPropertyChanged can provide the needed change events without any knowledge of the current view. This way the view doesn't depend directly on the model.

Futher reading: The role of the model in MVVM

Community
  • 1
  • 1
d_schnell
  • 614
  • 4
  • 5
  • Could you provide a sample on how would I go about retrieving changes from the CollectionView? The only events I see on the CollectionView are CurrentChanging / CurrentChanged and even those are fired only when I sort the collection, not when I change the selected cell. Thanks – Jan Kratochvil Feb 15 '12 at 08:45
0

I have found a very simple solution (can't belive I haven't seen that one) composed of catching two events from the DataGrid. Here is the code:

private object changedRow;

    private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        changedRow = e.Row.Item;
    }

    private void MainDataGrid_CurrentCellChanged(object sender, EventArgs e)
    {
        if (changedRow != null)
        {
            // do something with the edited row here
            changedRow = null;
        }
    }
Jan Kratochvil
  • 2,307
  • 1
  • 21
  • 39