Back to Devexpress

DxRichEdit.CalculateDocumentVariable Event

blazor-devexpress-dot-blazor-dot-richedit-dot-dxrichedit-68dda96f.md

latest3.9 KB
Original Source

DxRichEdit.CalculateDocumentVariable Event

Fires when a DOCVARIABLE field is updated and allows you to specify the field result.

Namespace : DevExpress.Blazor.RichEdit

Assembly : DevExpress.Blazor.RichEdit.v25.2.dll

NuGet Package : DevExpress.Blazor.RichEdit

Declaration

csharp
[Parameter]
public EventCallback<CalculateDocumentVariableEventArgs> CalculateDocumentVariable { get; set; }

Event Data

The CalculateDocumentVariable event's data class is CalculateDocumentVariableEventArgs. The following properties provide information specific to this event:

PropertyDescription
ArgumentsReturns DOCVARIABLE field arguments.
ValueSpecifies the result of the DOCVARIABLE field that fired the event.
VariableNameGets the name of the currently processed document variable.

Remarks

Handle the CalculateDocumentVariable event to calculate a value of the DOCVARIABLE field. The VariableName event argument’s property identifies the processed field. Set the Value property to the field result value.

razor
<DxRichEdit @bind-Selection="@selection"
            @ref="@richEdit"
            CalculateDocumentVariable="@(async x => await OnCalculateDocumentVariable(x))" />

@code {
    DxRichEdit richEdit { get; set; }
    Selection selection { get; set; }
    protected override Task OnAfterRenderAsync(bool firstRender) {
        if(firstRender)
            InitializeDocument();
        return base.OnAfterRenderAsync(firstRender);
    }

    async Task InitializeDocument() {
        /* Surround the code that contains an asynchronous operation with a try-catch block to handle
        the OperationCanceledException. This exception is thrown when an asynchronous operation is canceled. */
        var documentAPI = richEdit.DocumentAPI;
        documentAPI.BeginUpdate();
        try {
            await documentAPI.Paragraphs.CreateAsync();
            var textSpan = await documentAPI.AddTextAsync("Number of paragraphs: ");
            var field = await documentAPI.Fields.CreateAsync(textSpan.Interval.Start + textSpan.Interval.Length, "DOCVARIABLE paragraphCount");
            await documentAPI.Fields.UpdateAsync(field);
        } catch(OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
        documentAPI.EndUpdate();
    }
    async Task OnCalculateDocumentVariable(CalculateDocumentVariableEventArgs eventArgs) {
        try {
            switch(eventArgs.VariableName) {
                case "paragraphCount":
                    var paragraphs = await richEdit.DocumentAPI.Paragraphs.GetAllAsync();
                    eventArgs.Value = paragraphs.Count.ToString();
                    break;
                default:
                    break;
            }
        } catch(OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
    }
}

See Also

DxRichEdit Class

DxRichEdit Members

DevExpress.Blazor.RichEdit Namespace