locked
SvcConfigEditor and Collection Editor Dialog RRS feed

  • Question

  • In reviewing the sample on message inspectors (http://msdn2.microsoft.com/En-US/library/aa717047.aspx), I noticed that the SchemaValidationBehaviorExtensionElement has a collection of schemas. When adding this behavior with the SvcConfigEditor tool, you get a collection editor dialog but the Add and Remove buttons are disabled so you can't add the schema that you want to validate against. How would you modify the sample code to enable the Add button?

     

    Thanks,

     

    James

    Monday, June 18, 2007 3:57 PM

Answers

  • Please follow the steps below:

     

    1.     Implement you own editor to edit the SchemasCollection, which derives from the general CollectionEditor;

    a)      Make sure your constructor passes a type of class that is derived from IList to the base CollectionEditor.ctor, this is required to enable Add/Remove button;

            public SchemasCollectionEditor() : base(typeof(IList))

            {

            }

    b)      Override and implement necessary methods;

    2.     Apply EditorAttribute to SchemaValidationBehaviorExtensionElement.Schemas;

            [ConfigurationProperty("schemas", IsDefaultCollection = true)]

            [EditorAttribute(typeof(SchemasCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]

            public SchemasCollection Schemas

    3.     Now the Add/Remove buttons are enabled and you can use it to edit any SchemasCollection within SvcConfigEditor;

     

    Below is the sample source code for SchemasCollectionEditor.cs

     

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.Design;

    using System.Drawing.Design;

    using System.Text;

     

    namespace Microsoft.ServiceModel.Samples

    {

        public class SchemasCollectionEditor : CollectionEditor

        {

            public SchemasCollectionEditor() : base(typeof(IList))

            {

            }

     

            protected override object CreateInstance(Type itemType)

            {

                return new SchemaConfigElement();

            }

     

            protected override void DestroyInstance(object Instance)

            {

                return;

            }

     

            protected override bool CanRemoveInstance(object value)

            {

                return true;

            }

     

            protected override bool CanSelectMultipleInstances()

            {

                return false;

            }

     

            protected override Type CreateCollectionItemType()

            {

                return typeof(SchemaConfigElement);

            }

     

            protected override Type[] CreateNewItemTypes()

            {

                return new Type[] { typeof(SchemaConfigElement) };

            }

     

            protected override string GetDisplayText(object value)

            {

                SchemaConfigElement element = (SchemaConfigElement)value;

                return element.Location;

            }

     

            protected override Object[] GetItems(Object editValue)

            {

                SchemasCollection col = (SchemasCollection)editValue;

                Object[] array = new object[col.Count];

                for (int i = 0; i < col.Count; ++i)

                {

                    array[ i ] = col[ i ];

                }

                return array;

            }

     

            protected override object SetItems(object editValue, object[] value)

            {

                SchemasCollection col = (SchemasCollection)editValue;

                col.Clear();

                foreach (object o in value)

                {

                    SchemaConfigElement element = (SchemaConfigElement)o;

                    col.Add(element);

                }

                return editValue;

            }

        }

    }

     

    Thursday, July 12, 2007 12:51 PM

All replies

  • Please follow the steps below:

     

    1.     Implement you own editor to edit the SchemasCollection, which derives from the general CollectionEditor;

    a)      Make sure your constructor passes a type of class that is derived from IList to the base CollectionEditor.ctor, this is required to enable Add/Remove button;

            public SchemasCollectionEditor() : base(typeof(IList))

            {

            }

    b)      Override and implement necessary methods;

    2.     Apply EditorAttribute to SchemaValidationBehaviorExtensionElement.Schemas;

            [ConfigurationProperty("schemas", IsDefaultCollection = true)]

            [EditorAttribute(typeof(SchemasCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]

            public SchemasCollection Schemas

    3.     Now the Add/Remove buttons are enabled and you can use it to edit any SchemasCollection within SvcConfigEditor;

     

    Below is the sample source code for SchemasCollectionEditor.cs

     

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.Design;

    using System.Drawing.Design;

    using System.Text;

     

    namespace Microsoft.ServiceModel.Samples

    {

        public class SchemasCollectionEditor : CollectionEditor

        {

            public SchemasCollectionEditor() : base(typeof(IList))

            {

            }

     

            protected override object CreateInstance(Type itemType)

            {

                return new SchemaConfigElement();

            }

     

            protected override void DestroyInstance(object Instance)

            {

                return;

            }

     

            protected override bool CanRemoveInstance(object value)

            {

                return true;

            }

     

            protected override bool CanSelectMultipleInstances()

            {

                return false;

            }

     

            protected override Type CreateCollectionItemType()

            {

                return typeof(SchemaConfigElement);

            }

     

            protected override Type[] CreateNewItemTypes()

            {

                return new Type[] { typeof(SchemaConfigElement) };

            }

     

            protected override string GetDisplayText(object value)

            {

                SchemaConfigElement element = (SchemaConfigElement)value;

                return element.Location;

            }

     

            protected override Object[] GetItems(Object editValue)

            {

                SchemasCollection col = (SchemasCollection)editValue;

                Object[] array = new object[col.Count];

                for (int i = 0; i < col.Count; ++i)

                {

                    array[ i ] = col[ i ];

                }

                return array;

            }

     

            protected override object SetItems(object editValue, object[] value)

            {

                SchemasCollection col = (SchemasCollection)editValue;

                col.Clear();

                foreach (object o in value)

                {

                    SchemaConfigElement element = (SchemaConfigElement)o;

                    col.Add(element);

                }

                return editValue;

            }

        }

    }

     

    Thursday, July 12, 2007 12:51 PM
  • Thank you for your response and the code example. It definitely helped me to understand the type editor framework. I went ahead and built another type editor for the schema location property using the OpenFileDialog.

     

    Thanks again

     

    James

    Thursday, July 12, 2007 5:58 PM
  • I'm glad it helped. It's also nice to hear that you've implemented your own type editor for schema location.
    Friday, July 13, 2007 2:07 PM