wpf-120276-controls-and-libraries-rich-text-editor-examples-commands-how-to-replace-standard-dx-richedit-command-with-a-custom-command.md
The following example describes how to replace the built-in command with a custom one using the service substitution technique.
In this example, the CustomSaveDocumentCommand class is a SaveDocumentCommand descendant. It overrides the ExecuteCore method to display a message after a document has been saved.
public class CustomSaveDocumentCommand:SaveDocumentCommand {
public CustomSaveDocumentCommand(IRichEditControl control)
: base(control)
{
}
protected override void ExecuteCore()
{
base.ExecuteCore();
MessageBox.Show("Custom SaveDocument command was executed");
}
}
Public Class CustomSaveDocumentCommand
Inherits SaveDocumentCommand
Public Sub New(ByVal control As IRichEditControl)
MyBase.New(control)
End Sub
Protected Overrides Sub ExecuteCore()
MyBase.ExecuteCore()
MessageBox.Show("Custom SaveDocument command was executed")
End Sub
End Class
Implement the IRichEditCommandFactoryService descendant. Create a CreateCommand method to generate a new custom command object used to replace the built-in command.
public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService
{
readonly IRichEditCommandFactoryService service;
readonly RichEditControl control;
public CustomRichEditCommandFactoryService(RichEditControl control,
IRichEditCommandFactoryService service) {
Guard.ArgumentNotNull(control, "control");
Guard.ArgumentNotNull(service, "service");
this.control = control;
this.service = service;
}
public RichEditCommand CreateCommand(RichEditCommandId id) {
if(id == RichEditCommandId.FileSave)
return new CustomSaveDocumentCommand(control);
return service.CreateCommand(id);
}
}
Public Class CustomRichEditCommandFactoryService
Implements IRichEditCommandFactoryService
Private ReadOnly service As IRichEditCommandFactoryService
Private ReadOnly control As RichEditControl
Public Sub New(ByVal control As RichEditControl, ByVal service As IRichEditCommandFactoryService)
Guard.ArgumentNotNull(control, "control")
Guard.ArgumentNotNull(service, "service")
Me.control = control
Me.service = service
End Sub
Public Function CreateCommand(ByVal id As RichEditCommandId) As RichEditCommand Implements IRichEditCommandFactoryService.CreateCommand
If id = RichEditCommandId.FileSave Then
Return New CustomSaveDocumentCommand(control)
End If
Return service.CreateCommand(id)
End Function
End Class
In the RichEditControl.Loaded event handler, create a new IRichEditCommandFactoryService descendant instance and use the RichEditControl.ReplaceService<T> method to substitute the default service with the newly created service.
void richEditControl1_Loaded(object sender, RoutedEventArgs e) {
ReplaceRichEditCommandFactoryService(richEditControl1);
}
void ReplaceRichEditCommandFactoryService(RichEditControl control)
{
IRichEditCommandFactoryService service = control.GetService<IRichEditCommandFactoryService>();
control.ReplaceService<IRichEditCommandFactoryService>(new CustomRichEditCommandFactoryService(control, service));
}
Private Sub richEditControl1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
ReplaceRichEditCommandFactoryService(richEditControl1)
End Sub
Private Sub ReplaceRichEditCommandFactoryService(ByVal control As RichEditControl)
Dim service As IRichEditCommandFactoryService = control.GetService(Of IRichEditCommandFactoryService)()
control.ReplaceService(Of IRichEditCommandFactoryService)(New CustomRichEditCommandFactoryService(control, service))
End Sub