Edit this page

Customizing RadExpressionEditor

Creating custom expression functions

Although Telerik provides a great number of predefined functions, there could be scenarios, which require additional functionality. We provide a mechanism, which allows the developer to create a custom expression method, which can be used in RadExpressionEditor or as a value of the Expression property. In order to do that, create a class that inherits from the ExpressionContext class:

public class CustomExpressionContext : Telerik.Data.Expressions.ExpressionContext
    {
/// <summary>
/// My custom function, which returns Pi constant.
/// </summary>
/// <returns></returns>
public double Pi()
{
    return Math.PI;
}
    }

Public Class CustomExpressionContext
    Inherits Telerik.Data.Expressions.ExpressionContext
    ''' <summary>
    ''' My custom function, which returns Pi constant.
    ''' </summary>
    ''' <returns></returns>
    Public Function Pi() As Double
        Return Math.PI
    End Function
End Class

Once implemented, the new custom ExpressionContext class can be assigned by using the static Context property:

Telerik.Data.Expressions.ExpressionContext.Context = new CustomExpressionContext();

Telerik.Data.Expressions.ExpressionContext.Context = New CustomExpressionContext()

Than the new expression method can be used:

this.radGridView1.Columns["expression"].Expression = "PI()";

Me.RadGridView1.Columns("expression").Expression = "PI()"

Customizing the functions list

RadExpressionEditor supports loading functions, operators and constants from an XML file. This allows for customizing the available elements, localizing functions descriptions and adding custom functions. All available values should be described in the source XML file according to the following XML structure:

    <?xml version="1.0" encoding="utf-8" ?>
    <ExpressionItemsList>
      <!-- Custom functions -->
      <ExpressionItem Name="GetPi (Custom Function)" Value="PI()" Syntax="PI()" Type="OtherFunc">
        <Description>
          Get the value of Pi. This is a custom added function.
        </Description>
      </ExpressionItem>

      <ExpressionItem Name="Sum" Value="SUM()" Syntax="SUM([List(Of Number)])" Type="AggregateFunc">
        <Description>
          Computes the sum of a sequence of values.
        </Description>
      </ExpressionItem>
    </ExpressionItemsList>

To load prepared XML source file LoadFromXML method should be used as shown bellow:

string path = "Telerik.Examples.WinControls.GridView.Expressions.ExpressionItemsListData.xml";
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
RadExpressionEditorForm.ExpressionItemsList.LoadFromXML(stream);

Dim path As String = "Telerik.Examples.WinControls.GridView.Expressions.ExpressionItemsListData.xml"
Dim stream_ As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
RadExpressionEditorForm.ExpressionItemsList.LoadFromXML(stream_)

ExpressionEditorFormCreated event

This event allows you to easily access the expression editor form before it is shown to the user. The following example shows how you can change the BackColor and hide the icon in the title bar:

private void RadGridView1_ExpressionEditorFormCreated(object sender, ExpressionEditorFormCreatedEventArgs e)
{
    e.ExpressionEditorForm.FormElement.TitleBar.FillPrimitive.BackColor = Color.LightSkyBlue;
    e.ExpressionEditorForm.ShowIcon = false;
}

Private Sub RadGridView1_ExpressionEditorFormCreated(ByVal sender As Object, ByVal e As ExpressionEditorFormCreatedEventArgs)
    e.ExpressionEditorForm.FormElement.TitleBar.FillPrimitive.BackColor = Color.LightSkyBlue
    e.ExpressionEditorForm.ShowIcon = False
End Sub
'#endgion
Class
gion customFunction
ic Class CustomExpressionContext
Inherits Telerik.Data.Expressions.ExpressionContext
''' <summary>
''' My custom function, which returns Pi constant.
''' </summary>
''' <returns></returns>
Public Function Pi() As Double
    Return Math.PI
End Function
Class

See Also