在自定义编辑器中使用时,背景图像会引发异常

时间:2018-02-12 10:03:01

标签: c# winforms windows-forms-designer

我在自定义控件中为属性(包含自定义控件的集合)使用自定义编辑器集合。我已将此属性的UITypeEditor用于此link中。现在,当单击该属性时,UI编辑器会显示,但单击背景图像时会显示以下错误。

在自定义编辑器

中使用时,背景图像会抛出异常

enter image description here

有谁可以帮我解决这个问题?

这是我的财产

    /// <summary>
    /// Gets or sets the value to the items of the the TreeNavigator.
    /// </summary>
    [Browsable(true), DefaultValue(null),
    Category("Appearance"), Description("Gets or sets the value to the items of the Custom control.")]
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [TypeConverter(typeof(ItemConverter))]
    [Editor(typeof(ItemCustomCollectionEditor), typeof(UITypeEditor))]
    public MenuItemCollection Items
    {
        get
        {
            if (items == null)
            {
                items = new MenuItemCollection((this.Parent.Parent as CustomControl));
           }

            return items;
        }
        set
        {
            if (items != value)
            {
                items = value;
                PerformLayout();
            }
        }
    }

这是我的自定义编辑器

public class ItemCustomCollectionEditor : System.Drawing.Design.UITypeEditor
{
    /// <summary>
    /// delegate for collection changed event
    /// </summary>
    public delegate void CollectionChangedEventHandler(object sender, object instance, object value);

    /// <summary>
    /// Fires when the Collection in the item changes
    /// </summary>
    public event CollectionChangedEventHandler CollectionChanged;
    /// <summary>
    /// ITypeDescriptorContext
    /// </summary>
    private ITypeDescriptorContext _context;
    /// <summary>
    /// IWindowsFormsEditorService
    /// </summary>
    private IWindowsFormsEditorService edSvc = null;
    /// <summary>
    /// ItemCustomCollectionEditor constructor
    /// </summary>
    public ItemCustomCollectionEditor()
    { }
    /// <summary>
    /// EditVaue method is used to edit the value in the Item Collection
    /// </summary>
    /// <param name="context">Provides information about container</param>
    /// <param name="provider">Object providing custom support</param>
    /// <param name="value">Object of the container</param>
    /// <returns>Returns the object of the container</returns>
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        Menuitemcollection coll = value as Menuitemcollection;
        CustomControl owner = coll.container;
        if (context != null && context.Instance != null && provider != null)
        {
            object originalValue = value;
            _context = context;
            if (provider != null)
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            }
            if (edSvc != null)
            {
                ItemCollectionEditorForm collEditorFrm = CreateForm(owner);
                collEditorFrm.ItemAdded += new ItemCollectionEditorForm.InstanceEventHandler(ItemAdded);
                collEditorFrm.ItemRemoved += new ItemCollectionEditorForm.InstanceEventHandler(ItemRemoved);

                collEditorFrm.Collection = (IList)value;


                context.OnComponentChanging();
                if (edSvc.ShowDialog(collEditorFrm) == DialogResult.OK)
                {
                    OnCollectionChanged(context.Instance, value);
                    context.OnComponentChanged();
                    value = collEditorFrm.Collection;
                }
            }
        }

        return value;
    }

    /// <summary>
    /// GetEditStyle for the editor
    /// </summary>
    /// <param name="context">Provides information about container</param>
    /// <returns>Returns EditorStyle</returns> 
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        if (context != null && context.Instance != null)
        {
            return UITypeEditorEditStyle.Modal;
        }
        return UITypeEditorEditStyle.Modal;
    }
    /// <summary>
    /// Indicates the ItemAdded
    /// </summary>
    /// <param name="sender">Sender as container</param>
    /// <param name="item">Added Items in the container</param>

    private void ItemAdded(object sender, object item)
    {

        if (_context != null && _context.Container != null)
        {
            IComponent icomp = item as IComponent;
            if (icomp != null)
            {
                _context.Container.Add(icomp);
            }
        }

    }
    /// <summary>
    /// Indicates the ItemAdded
    /// </summary>
    /// <param name="sender">Sender as container</param>
    /// <param name="item">Added Items in the container</param>
    private void ItemRemoved(object sender, object item)
    {
        if (_context != null && _context.Container != null)
        {
            IComponent icomp = item as IComponent;
            if (icomp != null)
            {
                _context.Container.Remove(icomp);
            }
        }

    }
    /// <summary>
    /// Triggers the CollectionChanged event
    /// </summary>
    /// <param name="instance">Object that carries the Item</param>
    /// <param name="value">Value of the Item with all its associated properties</param>

    protected virtual void OnCollectionChanged(object instance, object value)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, instance, value);
        }
    }

    /// <summary>
    /// Creates the form
    /// </summary>
    /// <param name="owner">CustomControl Form</param>
    /// <returns>Returns the new CollectionEditorForm</returns>
    protected virtual ItemCollectionEditorForm CreateForm(CustomControl owner)
    {
        return new ItemCollectionEditorForm(owner);
    }


}

0 个答案:

没有答案