_XDocumentEventSink2_Event.OnContextChange event

Office 2013 and later

Occurs after the context node changes.

Namespace:  Microsoft.Office.Interop.InfoPath.SemiTrust
Assembly:  Microsoft.Office.Interop.InfoPath.SemiTrust (in Microsoft.Office.Interop.InfoPath.SemiTrust.dll)

event _XDocumentEventSink2_OnContextChangeEventHandler OnContextChange

The context node is the XML DOM node mapped to the view that corresponds to the container (or item) with the current XML selection. For example, if the current selection in the view is in a text box, the context node is the node to which the text box is bound. If the current selection is a repeating section, the context node is the node for that item. If two repeating sections are selected, the context node is the ancestor XML DOM for both items mapped to the view.

The OnContextChange event is asynchronous. It does not fire on every change in the context node; instead, it fires after the application has stopped processing other events.

When the document loads, or when a view change occurs, the OnContextChange event will occur after the OnLoadand OnSwitchView events occur.

When the IsUndoRedo property of the DocContextChangeEventObject object is true, the context change was caused by a user's undo or redo operation rather than an explicit user context change. Operations performed within the OnContextChange event that modify the XML DOM should be avoided in response to undo or redo operations, because they may interfere with the user's intention to revert data to a previous state.

For rich text box controls, the OnContextChange event is not raised for context changes within the XHTML content— that is, selection changes to the rich text in the control. The GetContextNodes method can be used to determine the selection within rich text box controls.

public void OnContextChange(DocContextChangeEvent e)
if (e.Type == "ContextNode")
IXMLNode expNode=XDocument.DOM.selectSingleNode("//my:field4");
// set field4 with the name of the current context.