Edytuj

Udostępnij za pośrednictwem


ExpressionEditor Class

Definition

Defines a set of properties and methods for evaluating an expression that is associated with a control property at design time and to provide an expression editor sheet to the visual design host for use in the expression editor dialog box. This class is abstract.

public ref class ExpressionEditor abstract
public abstract class ExpressionEditor
type ExpressionEditor = class
Public MustInherit Class ExpressionEditor
Inheritance
ExpressionEditor
Derived

Examples

The following code example demonstrates how to derive from the ExpressionEditor class to define a custom expression editor.

using System;
using System.Collections;
using System.Collections.Specialized;
using System.CodeDom;
using System.Configuration;
using System.Web.UI.Design;
using System.Web.Compilation;

namespace ExpressionEditorSamples.CS
{
    [ExpressionPrefix("CustomAppSettings")]
    [ExpressionEditor(typeof(ExpressionEditorSamples.CS.CustomAppSettingsEditor))]
    public class CustomAppSettingsBuilder : AppSettingsExpressionBuilder
    {
        // Use the built-in AppSettingsExpressionBuilder class,
        // but associate it with a custom expression editor class.
    }

    public class CustomAppSettingsEditor : System.Web.UI.Design.ExpressionEditor
    {
        public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
        {
            KeyValueConfigurationCollection customSettings = null;

            if (serviceProvider != null)
            {
                IWebApplication webApp = (IWebApplication)serviceProvider.GetService(typeof(IWebApplication));
                if (webApp != null)
                {
                    Configuration config = webApp.OpenWebConfiguration(true);
                    if (config != null)
                    {
                        AppSettingsSection settingsSection = config.AppSettings;
                        if (settingsSection != null)
                        {
                            customSettings = settingsSection.Settings;
                        }
                    }
                }
            }

            if (customSettings != null)
            {
                return customSettings[expression];
            }

            return expression;
        }
    }
}

Remarks

A visual designer host, such as Visual Studio 2005, uses the ExpressionEditor class to present custom expression editor sheets to the user, and then evaluate the selected expression for design-time rendering.

When you browse the Expressions property for a control in the design-time Properties grid, the visual designer displays a dialog box to set expressions for a control property. You can select the expression type based on a list of expression prefixes. When you select an expression prefix from the list, the visual designer uses the associated ExpressionEditor and ExpressionEditorSheet objects to set, evaluate, and convert the expression string based on the syntax for that type of expression. The visual designer sets the expression for the associated control property, and then uses the evaluated expression result to assign control property values that are rendered on the design surface.

The static GetExpressionEditor methods get the expression editor that is associated with a particular expression prefix or expression builder. The ExpressionPrefix property for an ExpressionEditor object returns the configured expression prefix. The EvaluateExpression method evaluates an input expression string. The GetExpressionEditorSheet method returns the ExpressionEditorSheet implementation that is used to prompt for the custom expression properties in the expressions dialog box.

Typically, to support a new expression type at design time, you define a unique expression prefix and provide custom ExpressionBuilder and ExpressionEditor implementations. Optionally, you can provide a custom ExpressionEditorSheet implementation that defines properties that are used to form the expression in the expressions dialog box.

The expression prefix identifies the custom expression type and associates an expression with the expression builder and expression editor. When custom expressions are parsed in a page, the expression prefix is used to create instances of the associated ExpressionBuilder and ExpressionEditor classes. To associate an expression prefix with an expression builder and expression editor, apply the ExpressionEditorAttribute and ExpressionPrefixAttribute attributes to the custom ExpressionBuilder class and configure the expression prefix for an expression builder in the expressionBuilders element in the Web configuration file. The prefix is not required, but highly recommended.

Notes to Implementers

The following steps are required in deriving a custom ExpressionEditor class:

For example, the ResourceExpressionEditor class derives from the ExpressionEditor class and provides an implementation for evaluating and associating a resource string reference with a control property at design time. The ResourceExpressionBuilder class is associated with the expression prefix Resources and the ResourceExpressionEditor implementation. The GetExpressionEditorSheet(String, IServiceProvider) method returns a ResourceExpressionEditorSheet, which defines the individual properties that form a resource reference expression.

Constructors

ExpressionEditor()

Initializes a new instance of the ExpressionEditor class.

Properties

ExpressionPrefix

Gets the expression prefix that identifies expression strings that are supported by the expression editor implementation.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
EvaluateExpression(String, Object, Type, IServiceProvider)

Evaluates an expression string and provides the design-time value for a control property.

GetExpressionEditor(String, IServiceProvider)

Returns an ExpressionEditor implementation that is associated with the specified expression prefix.

GetExpressionEditor(Type, IServiceProvider)

Returns an ExpressionEditor implementation that is associated with the specified expression builder type.

GetExpressionEditorSheet(String, IServiceProvider)

Returns an expression editor sheet that is associated with the current expression editor.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also