Custom Functions in the Expression Editor (Blazor WebAssembly Hosted App)
- 3 minutes to read
This topic describes how to add a custom function to the Expression Editor or remove a function from the list of available functions in the Report Designer.
The custom function described in this topic is available in the Expression Editor invoked from the Report Designer. If you want your custom function to be available for SQL queries in the Data Source Wizard or elsewhere in the application outside of Report components, implement the ICustomFunctionOperatorFormattable interface and register the function with the CriteriaOperator.RegisterCustomFunction method.
Review the following help topic for more information: Custom Functions.
Add a Custom Function
Implement a Custom Function
To create a custom function, create an object that descends from the ReportCustomFunctionOperatorBase abstract class.
The following code defines a custom function CustomFormatFunction(string format, object arg0)
:
using DevExpress.XtraReports.Expressions;
using System;
public class CustomFormatFunction : ReportCustomFunctionOperatorBase
{
public override string FunctionCategory
=> "Custom";
public override string Description
=> "CustomFormatFunction(string format, object arg0)" +
"\r\nConverts an arg0 value to a string based on a specified format";
public override bool IsValidOperandCount(int count)
=> count == 2;
public override bool IsValidOperandType(int operandIndex, int operandCount, Type type)
=> true;
public override int MaxOperandCount
=> 2;
public override int MinOperandCount
=> 2;
public override object Evaluate(params object[] operands)
{
string res = String.Format(operands[0].ToString(), operands[1]);
return res;
}
public override string Name
=> "CustomFormatFunction";
public override Type ResultType(params Type[] operands)
{
return typeof(string);
}
}
Register a Function
Call the CustomFunctions.Register method at application startup:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// ...
DevExpress.XtraReports.Expressions.CustomFunctions.Register(new CustomFormatFunction());
// ...
await builder.Build().RunAsync();
Result
The following image shows a custom function (CustomFormatFunction
) in the list of available functions in the Expression Editor:
Remove a Function from the List of Available Functions
Handle the BeforeRender event and remove a function from the DevExpress.Reporting.Designer.Widgets.reportFunctionDisplay collection. To get access to a function in the event handler, use the following notation where index is an index in the function category list (Aggregate, DateTime, Logical, Math, and so on):
s.dx.Reporting.Designer.Widgets.reportFunctionDisplay[index].items["function_name"]
The following JavaScript code sample removes the LocalDateTimeThisYear function from the DateTime category. Insert this code snippet in the head section of the wwwroot/index.html
file in the project.
<script type="text/javascript">
window.designerCustomization = {
onBeforeRender: function (s, e) {
delete s.dx.Reporting.Designer.Widgets.reportFunctionDisplay[1].items["LocalDateTimeThisYear"];
}
};
</script>
Subscribe to the BeforeRender event:
<DxReportDesigner ReportName="LargeDatasetReport" Height="calc(100vh - 130px)" Width="100%" AllowMDI="true" DataSources="DataSources">
<DxReportDesignerWizardSettings UseFullscreenWizard="true" />
<DxReportDesignerCallbacks BeforeRender="designerCustomization.onBeforeRender" />
</DxReportDesigner>
Unregister a Function
If you remove a function from the DevExpress.Reporting.Designer.Widgets.reportFunctionDisplay collection on the client, the function remains recognized, and expressions with that function remain valid and can be calculated. To compose a new expression manually, type a function name.
If you have registered a custom function, you can call the CustomFunctions.Unregister method at application startup to unregister it:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// ...
DevExpress.XtraReports.Expressions.CustomFunctions.Unregister("CustomFormatFunction");
// ...
await builder.Build().RunAsync();
The CustomFormatFunction
function is not available in the Expression Editor and cannot be evaluated in expressions after this code is run.