C# PropertyGrid中的CheckListBox属性集合
我有一个UITypeEditor类,因此在PropertyGrid属性集合中是CheckListBox。例如,我在杂货店列表中创建了一个产品类别(水果、蔬菜),以及一个我需要选择属于该类别的产品的所有产品的列表。那些我添加了一个空的杂货店列表新的类别“水果”,这个类别是从这些产品的全球列表中选择的,其中包括:“苹果”、“梨”、“香蕉”。接下来,我想创建另一个类别,例如“蔬菜”,然后从一些蔬菜的全局列表中进行选择。问题是,在建立后一个类别之后,所有其他类别都会收到持续时间相同的一组产品。代码如下:C# PropertyGrid中的CheckListBox属性集合,c#,propertygrid,checklistbox,C#,Propertygrid,Checklistbox,我有一个UITypeEditor类,因此在PropertyGrid属性集合中是CheckListBox。例如,我在杂货店列表中创建了一个产品类别(水果、蔬菜),以及一个我需要选择属于该类别的产品的所有产品的列表。那些我添加了一个空的杂货店列表新的类别“水果”,这个类别是从这些产品的全球列表中选择的,其中包括:“苹果”、“梨”、“香蕉”。接下来,我想创建另一个类别,例如“蔬菜”,然后从一些蔬菜的全局列表中进行选择。问题是,在建立后一个类别之后,所有其他类别都会收到持续时间相同的一组产品。代码如下:
public class CheckedListBoxUiTypeEditor : UITypeEditor
{
private readonly CheckedListBox _checklisbox1 = new CheckedListBox();
private IWindowsFormsEditorService _es;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override bool IsDropDownResizable => true;
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (provider != null)
{
_es = provider.GetService(typeof (IWindowsFormsEditorService)) as IWindowsFormsEditorService;
}
if (_es != null)
{
LoadValues(value);
_es.DropDownControl(_checklisbox1);
}
_result.Clear();
foreach (string str in _checklisbox1.CheckedItems)
{
_result.Add(str);
}
return _result;
}
private readonly List<string> _defaultList = FormLas.ListAll;
private readonly List<string> _result = new List<string>();
private void LoadValues(object value)
{
Hashtable table = new Hashtable();
foreach (string str in _defaultList)
{
table.Add(str, false);
}
_checklisbox1.Items.Clear();
foreach (DictionaryEntry dic in table)
{
_checklisbox1.Items.Add(dic.Key, (bool) dic.Value);
}
if (((List<string>) value).Count > 0)
{
foreach (string str in (List<string>)value)
{
for (int i = 0; i < _checklisbox1.Items.Count; i++)
{
if (str == _checklisbox1.Items[i])
{
_checklisbox1.SetItemChecked(i, true);
}
}
}
}
}
}
公共类CheckedListBoxUiTypeEditor:UITypeEditor
{
private readonly CheckedListBox _checklisbox1=new CheckedListBox();
私人iWindows FormsEditor服务;
公共重写UITypeEditorEditStyle GetEditStyle(ITTypeDescriptorContext上下文)
{
返回UITypeEditorEditStyle.DropDown;
}
public override bool isdropdownResizeble=>true;
公共重写对象EditValue(ITypeDescriptorContext上下文、IServiceProvider提供程序、对象值)
{
if(提供程序!=null)
{
_es=provider.GetService(typeof(IWindowsFormsEditorService))作为IWindowsFormsEditorService;
}
如果(_es!=null)
{
荷载值(值);
_es.下拉控件(_checklisbox1);
}
_result.Clear();
foreach(checklisbox1.CheckedItems中的字符串str)
{
_结果:添加(str);
}
返回结果;
}
私有只读列表_defaultList=FormLas.ListAll;
私有只读列表_result=new List();
私有void加载值(对象值)
{
Hashtable=新的Hashtable();
foreach(默认列表中的字符串str)
{
表.添加(str,false);
}
_checklisbox1.Items.Clear();
foreach(字典在表中输入dic)
{
_勾选Lisbox1.项目添加(dic.Key,(bool)dic.Value);
}
如果((列表)值).Count>0)
{
foreach(列表值中的字符串str)
{
对于(int i=0;i<\u checklisbox1.Items.Count;i++)
{
if(str==\u checklisbox1.Items[i])
{
_checklisbox1.SetItemChecked(i,true);
}
}
}
}
}
}
发现错误,问题是全局变量(\u defaultList,\u result),这些变量的初始化必须放在body中。对_checklisbox1对象也应该这样做。如何初始化默认列表,“这些变量的初始化必须放在主体技术中”没有意义。我希望有一个集合属性,将此编辑器作为属性,以便在propertygrid中显示为检查表。