Providing UITypeEditor in DSL

Hi all,

We created a DSL that contains several objects with some very specific properties (e.g. a reference to a property of a class in an assembly).

For setting the value of a property (in debugging mode) I would like to provide a specialized UITypeEditor (e.g to load an assembly, pick a class and select one of the properties of the class).

Assuming it's possible, is there some documentation about how to provide a UITypeEditor with the DSL

Thanks in advance,

Ronald



Answer this question

Providing UITypeEditor in DSL

  • GrantStephen

    Hi Duncan,

    Think your answer will help me out!

    B.T.W. you're right by seaching the forum on UITypeEditor, sorry I should have done that first. Thanks for your extensive reply.

    Kind regards,
    Ronald


  • Saverain

    Ronald,

    Here is a quick overview of how to add a custom editor.  There have been a couple of other postings about this in the forum on the subject: just search for UITypeEditor.

    Regards, Duncan

     

    Providing a custom editor for a property
    1) Write your custom editor class (outline code below). Define this class in the Domain Model project, otherwise you will run into the cyclic reference problems described in this posting: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=188695&SiteID=1.

    2) add a custom attribute to the property value in the Domain Model. Do this by opening the domain model then viewing the properties for property value you want to specify the custom editor for. Click on the ellipsis against the "CLR Attributes" to display the custom editor form, and enter the appropriate values e.g.

    Attribute type: System.ComponentModel.Editor

    Parameters: typeof(Example.CustomUIEditor.ExampleUITypeEditor),typeof(System.Drawing.Design.UITypeEditor)

    3) Save the model and Transform all of the templates.
    The generated property should now have the UITypeEditor attribute against it.

     

    Example code

    using System;
    using System.Collections.Generic;
    using System.Text;

    // Need to add a reference to System.Drawing DLL.
    using System.Drawing.Design;
    using System.Security.Permissions;
    using Microsoft.VisualStudio.Modeling;

    namespace Example.CustomUIEditor
    {

     // FxCop rule: must have same security demands as parent class
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
        public class ExampleUITypeEditor : System.Drawing.Design.UITypeEditor
        {

            /// <summary>
            /// Overridden to specify that our editor is a modal form
            /// </summary>
            /// <param name="context"></param>
            /// <returns></returns>
            public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }


            /// <summary>
      /// Called by VS whenever the user clicks on the ellipsis in the
      /// properties window for a property to which this editor is linked.
      /// </summary>
      /// <param name="context"></param>
      /// <param name="provider"></param>
      /// <param name="value"></param>
      /// <returns></returns>
            public override object EditValue(
                System.ComponentModel.ITypeDescriptorContext context,
                IServiceProvider provider,
                object value)
            {

                // Get a reference to the underlying property element
                ElementPropertyDescriptor descriptor = context.PropertyDescriptor as ElementPropertyDescriptor;
                ModelElement underlyingModelElent = descriptor.ModelElement;

                // context.Instance also returns a model element, but this will either
                // be the shape representing the underlying element (if you selected
                // the element via the design surface), or the underlying element
                // itself (if you selected the element via the model explorer)
                ModelElement element = context.Instance as ModelElement;

                //TODO: create and show your form here. If the user has changed
                // the property value via the form, then update the "value"
                // variable.

                return value;
            }

        }

    }

     


  • Ray2006

    You're welcome, Ronald.

    It wasn't meant as a criticism; I just wanted to point out that there was more info available (although I don't think any of the other postings included a code sample).

    Kind regards,

    Duncan


  • Providing UITypeEditor in DSL