Datagrid event after binding is updated

Nov 6, 2008 at 4:52 PM
I'm using the DataGrid and need to call a method after a value has been edited via the cell. For most WPF controls I would set NotifyOnTargetUpdated=True in the binding and hook the TargetUpdated event in my Control. Unfortunately the DataGrid does not have a TargetUpdated event that I can see ( is there documentation somewhere? ). There is a CellEditEnding event but that apparently gets called immediately before the Binding Target value is changed. Is there a way to register a callback which is called after the Binding Target is updated?
Coordinator
Nov 6, 2008 at 6:20 PM
If all your care about is to have a method or operation fire after CellEditEnding or RowEditEnding, you can use the Dispatcher.BeginInvoke method in your Cell/RowEditEnding handler.
Coordinator
Nov 6, 2008 at 6:46 PM
Edited Nov 6, 2008 at 6:47 PM
Also if you want to execute code immediately after binding update then you can use the NotifyOnSourceUpdated approach ( I guess this is what you meant when you said NotifyOnTargetUpdate, because here the target is the cell's control and source is data object's property). Here are the details of this approach....

Set NotifyOnSourceUpdated to true on which ever binding you want to hook up to.

In code behind include the following line in constructor...
    Binding.AddSourceUpdatedHandler(<DataGridInstance>,OnDataGridSourceUpdated);

Following is the implementation of the eventhandler

        void OnDataGridSourceUpdated(object sender, DataTransferEventArgs e)

        {

            DataGridCell cell = FindVisualParent<DataGridCell>(e.TargetObject as UIElement);
            // Now you have the cell you are dealing with, so you can do what ever you want from here

        }

 

        static T FindVisualParent<T>(UIElement element) where T : UIElement

        {

            UIElement parent = element;

            while (parent != null)

            {

                T correctlyTyped = parent as T;

                if (correctlyTyped != null)

                {

                    return correctlyTyped;

                }

 

                parent = VisualTreeHelper.GetParent(parent) as UIElement;

            }

 

            return null;

        }

Nov 6, 2008 at 8:10 PM
Thanks, the AddSourceUpdatedHandler approach works fine. Is there a place for feature requests for the DataGrid? I think it would be nice if it worked like the other Controls and had a SourceUpdated event that could be attached to.