views:

145

answers:

1

Does anyone know how to control the the insertion mode of a WPF RichTextBox. I want to force the RichTextBox to always be in overwrite mode rather than insert.

A: 

Unfortunately there doesn't seem to be a documented way of doing this. The only way I know is to use reflection, as below, but this technique accesses internal workings of the RichTextBox. It works in current versions of WPF but there is no guarantee that it will continue to work going forward, so use it at your own risk.

PropertyInfo textEditorPropertyInfo = typeof(RichTextBox).GetProperty("TextEditor", BindingFlags.NonPublic | BindingFlags.Instance);

        if (textEditorPropertyInfo == null)
            throw new NotSupportedException("SetOverwriteable not support on this platform");

        object textEditor = textEditorPropertyInfo.GetValue(this, null);
        PropertyInfo overtypeModePropertyInfo = textEditor.GetType().GetProperty("_OvertypeMode", BindingFlags.NonPublic | BindingFlags.Instance);

        if (overtypeModePropertyInfo == null)
            throw new NotSupportedException("SetOverwriteable not support on this platform");

        overtypeModePropertyInfo.SetValue(textEditor, true, null);

The above needs to happen after the constructor.

Kid Kaneda