How to: Access the Rich Text Editor Controls
- 4 minutes to read
This topic describes how to access and customize the following controls in XAF applications with the Office Module:
Platform | Control | Purpose |
---|---|---|
WinForms | Used to show Rich Text documents | |
ASP.NET Core Blazor | Used to show Rich Text documents | |
ASP.NET Web Forms | Used to show Rich Text documents | |
ASP.NET Web Forms | Used to upload Rich Text documents on a server |
Access the WinForms-Specific Control (RichEditControl)
Follow the steps below to access the RichEditControl
and enable the Spell Check function.
- Create a custom ViewController in the WinForms module project (MySolution.Module.Win). If your solution does not contain this project, add the Controller to the WinForms application project (MySolution.Win).
- Add the DevExpress.SpellChecker.v24.1.dll and DevExpress.XtraSpellChecker.v24.1.dll assemblies to the project references.
- Override the
OnActivated
method and handle theRichTextServiceController.CustomizeRichEditControl
event. In the event handler, create and set up the SpellChecker object and pass this object to the SpellChecker property.
File:
MySolution.Win\Controllers\WinRichEditController.cs in solutions without the WinForms-specific module project.
MySolution.Module.Win\Controllers\WinRichEditController.cs(.vb) in solutions with the WinForms-specific module project.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Win;
using DevExpress.XtraRichEdit;
using DevExpress.XtraSpellChecker;
public partial class WinRichEditController : ViewController {
// ...
protected override void OnActivated() {
base.OnActivated();
RichTextServiceController controller = Frame.GetController<RichTextServiceController>();
if(controller != null) {
controller.CustomizeRichEditControl += Controller_CustomizeRichEditControl;
}
}
private void Controller_CustomizeRichEditControl(object sender, CustomizeRichEditEventArgs e) {
SpellChecker spellChecker = new SpellChecker();
spellChecker.SetSpellCheckerOptions(e.RichEditControl, new OptionsSpelling());
spellChecker.SpellCheckMode = SpellCheckMode.AsYouType;
e.RichEditControl.SpellChecker = spellChecker;
}
}
Note
The English dictionary is the default for the Spell Checker. Refer to the Dictionaries topic to see how to change the dictionary.
Access ASP.NET Web Forms-Specific Controls
Access the ASPxRichEdit Control
Follow the steps below to access the ASPxRichEdit
control and enable the Spell Check function.
- Create a custom ViewController in the ASP.NET Web Forms module project (MySolution.Module.Web).
- Add the DevExpress.Web.ASPxSpellChecker.v24.1.dll assembly to the project references.
- Override the
OnActivated
method and handle the ASPxRichTextPropertyEditor.ControlCreated event. In the event handler, create and set up the ASPxSpellCheckerOpenOfficeDictionary object and add this object to the Dictionaries collection. Set the Enabled property totrue
to enable the Spell Checker component.
File: MySolution.Module.Web\Controllers\WebRichEditController.cs(.vb).
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Web;
using DevExpress.Web.ASPxRichEdit;
using DevExpress.Web.ASPxSpellChecker;
using System;
using System.Globalization;
// ...
public class WebRichEditController : ViewController<DetailView> {
protected override void OnActivated() {
base.OnActivated();
foreach(ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
if(editor.ASPxRichEditControl != null) {
CustomizeRichEditControl(editor.ASPxRichEditControl);
} else {
editor.ControlCreated += Editor_ControlCreated;
}
}
}
private void Editor_ControlCreated(object sender, EventArgs e) {
ASPxRichEdit richEdit = ((ASPxRichTextPropertyEditor)sender).ASPxRichEditControl;
CustomizeRichEditControl(richEdit);
}
private void CustomizeRichEditControl(ASPxRichEdit richEdit) {
ASPxSpellCheckerOpenOfficeDictionary dictionary = new ASPxSpellCheckerOpenOfficeDictionary();
dictionary.GrammarPath = "~/App_Data/Dictionaries/en_US.aff";
dictionary.DictionaryPath = "~/App_Data/Dictionaries/en_US.dic";
dictionary.Culture = new CultureInfo("en-US");
dictionary.CacheKey = "enDic";
richEdit.Settings.SpellChecker.Dictionaries.Add(dictionary);
richEdit.Settings.SpellChecker.Enabled = true;
richEdit.Settings.SpellChecker.SuggestionCount = 4;
}
protected override void OnDeactivated() {
base.OnDeactivated();
foreach(ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
editor.ControlCreated -= Editor_ControlCreated;
}
}
}
Access the ASPxUploadControl
Follow the steps below to access the ASPxUploadControl
and set the maximum size of the file to be uploaded to the server.
- Create a custom ViewController in the ASP.NET Web Forms module project (MySolution.Module.Web).
- In the overridden
OnActivated
method, handle the ASPxSpreadsheetPropertyEditor.ControlCreated event. - In the event handler, use the MaxFileSize property to specify the maximum size of the file to be uploaded to the server.
File: MySolution.Module.Web\Controllers\WebRichEditUploadController.cs(.vb).
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Web;
using DevExpress.Web;
using System;
// ...
public class WebRichEditUploadController : ViewController<DetailView> {
protected override void OnActivated() {
base.OnActivated();
foreach (ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
if (editor.ASPxUploadControl != null) {
CustomizeRichEditUploadControl(editor.ASPxUploadControl);
}
else {
editor.ControlCreated += Editor_ControlCreated;
}
}
}
private void Editor_ControlCreated(object sender, EventArgs e) {
ASPxUploadControl richEditUploadControl = ((ASPxRichTextPropertyEditor)sender).ASPxUploadControl;
CustomizeRichEditUploadControl(richEditUploadControl);
}
private void CustomizeRichEditUploadControl(ASPxUploadControl uploadControl) {
uploadControl.ValidationSettings.MaxFileSize = 104857600;
}
protected override void OnDeactivated() {
base.OnDeactivated();
foreach (ASPxRichTextPropertyEditor editor in View.GetItems<ASPxRichTextPropertyEditor>()) {
editor.ControlCreated -= Editor_ControlCreated;
}
}
}
Access the ASP.NET Core Blazor-Specific Control’s Component Model (DxRichEditModel)
Follow these steps to access the DxRichEditModel
component model. A component model defines a Blazor component in code. When you modify the model, the underlying component reflects these changes. For more information about component models, refer to the following topic: Underlying Controls and Components Behind UI Elements (ASP.NET Core Blazor).
- Create a custom ViewController in the ASP.NET Core Blazor module project (MySolution.Module.Blazor). If your solution does not contain this project, add the Controller to the application project (MySolution.Blazor.Server).
- In the Controller’s
OnActivated
method, use the CustomizeViewItemControl<T>(DetailView, Controller, Action<T>) extension method to customize the RichTextPropertyEditor. - In the event handler, access the
DevExpress.ExpressApp.Office.Blazor.Components.Models.DxRichEditModel
component model and specify itsViewType
property to change a document view layout type (see ViewType).
File:
MySolution.Blazor.Server\Controllers\BlazorRichEditController.cs in solutions without the ASP.NET Core Blazor-specific module project.
MySolution.Module.Blazor\Controllers\BlazorRichEditController.cs in solutions with the ASP.NET Core Blazor-specific module project.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Office.Blazor.Editors;
//...
public class BlazorRichEditController : ViewController<DetailView> {
protected override void OnActivated() {
base.OnActivated();
View.CustomizeViewItemControl<RichTextPropertyEditor>(this, (propertyEditor) => {
propertyEditor.ComponentModel.ViewType = DevExpress.Blazor.RichEdit.ViewType.Simple;
});
}
}