Commit changes when the value of editor changes

John Doe / Friday, March 12, 2010

All of the ValueEditors that are used inside or outside the XamDataGrid by default have two modes - in EditMode and out of EditMode. The values are not committed until the value editor has ended its edit mode. When the value editors are embedded in the XamDataGrid they end edit mode when they lose focus or when the enter key is pressed.

 

If you want this value to be committed when the value is changed, then you have to handle the ValueChanged event of the value editor and for the editor to end edit mode. You can do this by calling the EndEditMode(...) method.

Subscribing to the ValueChanged event you can do with the EventManager class, like this:

                EventManager.RegisterClassHandler(typeof(ValueEditor),

                    ValueEditor.ValueChangedEvent,

                    new RoutedPropertyChangedEventHandler<object>(OnValueChanged));

This will be only applicable for editors like XamComboEditor, XamCheckEditor or XamDateTimeEditor.

You would not have to apply this to the rest of the editors as this will exit the EditMode each time you press a key:

        void OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

        {

            if (sender is XamComboEditor || sender is XamCheckEditor || sender is XamDateTimeEditor)

            {

                (sender as ValueEditor).EndEditMode(true, true);

            }

        }

In the attachments you can find the full source code.

XamDataGrid_ValueChanged.zip