为什么在集合值中收集本身而不是Item? [英] Why in collectioneditor value comes as collection itself instead of Item?

查看:75
本文介绍了为什么在集合值中收集本身而不是Item?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了自定义集合和自定义集合编辑器,用于在Windows窗体应用程序的设计器级别编辑集合。



但是当我点击收藏编辑器的添加按钮时,该值将被检索为收集本身而不是项目。



以下是我的代码部分,

 public MyItem 
{
.....
}

[EditorAttribute (typeof(MyItemCollectionEditor),typeof(UITypeEditor))]
[TypeConverter(typeof(MyItemCollectionConverter))]
public MyItemCollection :: ICollection,IDisposable,ICloneable,IList,ICustomTypeDescriptor
{

//实现了所有接口。

int IList.Add(对象值)
{
Add((MyItem)value); //抛出异常,因为"无法将MyItemCollection的类型转换为MyItem"

//为什么itemcollection作为值而不是item。
}

}



公共类MyItemCollectionEditor:CollectionEditor
{
public MyCollectionEditor(Type type)
:base(type)
{
}

protected override类型CreateCollectionItemType()
{
return typeof(MyItemCollection);
}

protected override bool CanSelectMultipleInstances()
{
return false;
}

公共覆盖对象EditValue(ITypeDescriptorContext context,IServiceProvider provider,object value)
{
return base.EditValue(context,provider,value);
}
}


公共类MyItemCollectionConverter:ExpandableObjectConverter
{

公共覆盖PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,object value ,Attribute [] attributes)
{
return((ICustomTypeDescriptor)value)。GetProperties(attributes);
}

公共覆盖bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return context == null || context.PropertyDescriptor == null
|| (context.Instance!= null&&& GetCount(context.PropertyDescriptor.GetValue(context.Instance))> 0);
}

公共覆盖/ * TypeConverter * / bool CanConvertTo(ITypeDescriptorContext context,Type destinationType)
{
if(destinationType == typeof(string))
{
返回true;
}
else
{
return base.CanConvertTo(context,destinationType);
}
}

int GetCount(对象值)
{
if(value == null)
{
return 0;
}

return((ICustomTypeDescriptor)value)。GetProperties(null).Count;
}

公共覆盖/ * TypeConverter * / object ConvertTo(ITypeDescriptorContext上下文,CultureInfo文化,对象值,类型destinationType)
{
if(destinationType == typeof) (字符串))
{
int count = GetCount(value);
返回计数> 0? String.Format(" Count = {0}",count):string.Empty;
}

返回base.ConvertTo(context,culture,value,destinationType);
}

}




任何人都可以让我知道,为什么项目集合是为了添加而不是项目?



委托,b $ b $ A Amal Raj U.



Amal Raj U

解决方案

也许你应该写:


    受保护的覆盖类型CreateCollectionItemType()

   {

      返回typeof(MyItem);

   }


Hi All,

I have created Custom collection and custom collection editor for editing the collection in designer level for windows form application.

But when i click in the Add button of collection editor the value is retrieved as Collection itself instead of Item.

Below is my code part,

    public MyItem 
    {
     .....
    }
 
    [EditorAttribute(typeof(MyItemCollectionEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(MyItemCollectionConverter))]
    public MyItemCollection : : ICollection, IDisposable, ICloneable, IList, ICustomTypeDescriptor
    {

         //implemented all the interfaces.

         int IList.Add(object value)
         {
             Add((MyItem)value);  //Exception thrown as "could not convert type of MyItemCollection to MyItem"

             //Why itemcollection coming as value instead of item.
         }
      
    }



    public class MyItemCollectionEditor : CollectionEditor
    {
    public MyCollectionEditor(Type type)
        : base(type)
    {
    }

    protected override Type CreateCollectionItemType()
    {
        return typeof(MyItemCollection);
    }

    protected override bool CanSelectMultipleInstances()
    {
        return false;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        return base.EditValue(context, provider, value);
    }
    }


    public class MyItemCollectionConverter : ExpandableObjectConverter
    {

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        return ((ICustomTypeDescriptor)value).GetProperties(attributes);
    }

    public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    {
        return context == null || context.PropertyDescriptor == null
            || (context.Instance != null && GetCount(context.PropertyDescriptor.GetValue(context.Instance)) > 0);
    }

    public override /*TypeConverter*/ bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            return true;
        }
        else
        {
            return base.CanConvertTo(context, destinationType);
        }
    }

    int GetCount(object value)
    {
        if (value == null)
        {
            return 0;
        }

        return ((ICustomTypeDescriptor)value).GetProperties(null).Count;
    }

    public override /*TypeConverter*/ object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            int count = GetCount(value);
            return count > 0 ? String.Format("Count = {0}", count) : string.Empty;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    } 

    }


Could anyone please let me know, why the item collection is came for adding instead of item?

Regards,
Amal Raj U.


Amal Raj U

解决方案

Maybe you should write:

   protected override Type CreateCollectionItemType()
   {
      return typeof(MyItem);
   }


这篇关于为什么在集合值中收集本身而不是Item?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆