How to force insert mode in WPF RichTextBox

Does anyone know how to control the WPF RichTextBox insert mode. I want to force the RichTextBox to always be in overwrite mode and not insert.

+1


a source to share


1 answer


Unfortunately, it looks like there is no documented way to do it. The only way I know is to use reflection as shown below, but this method addresses the inner workings of the RichTextBox. It works in current versions of WPF, but there is no guarantee that it will continue to work in the future, 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 should happen after the constructor.

0


a source







All Articles