有没有办法在属性网格之外使用CollectionEditor?我正在用可以让我更好地自定义UI的东西替换我的属性网格。我在我的表单上放了一个按钮,我希望点击它时会弹出一个CollectionEditor并允许我修改我的代码。当我使用PropertyGrid时,我需要做的就是向指向我的CollectionEditor的属性添加一些属性,它就可以工作了。但是如何手动调用CollectionEditor呢?谢谢!在这里找到答案:http://www.devnewsgroups.net/windowsforms/t11948-collectioneditor.aspx万一上面链接的网站有一天消失了,这里是它的要点。但是,代码是从上面的链接中逐字记录的;评论是我的。假设您有一个带有ListBox和按钮的窗体。如果要使用CollectionEditor编辑ListBox中的项目,可以在EventHandler中进行如下操作:privatevoidbutton1_Click(objectsender,System.EventArgse){//listBox1是包含集合的对象。请记住,如果集合//属于您正在编辑的类,您可以使用//Items是您希望编辑的集合的属性名称。PropertyDescriptorpd=TypeDescriptor.GetProperties(listBox1)["项目"];UITypeEditor编辑器=(UITypeEditor)pd.GetEditor(typeof(UITypeEditor));RuntimeServiceProviderserviceProvider=newRuntimeServiceProvider();editor.EditValue(serviceProvider,serviceProvider,listBox1.Items);现在,您需要做的下一件事是创建RuntimeServiceProvider()。这是上面链接中的海报用户来实现这个代码:);}返回空值;}classWindowsFormsEditorService:IWindowsFormsEditorService{#regionIWindowsFormsEditorServiceMemberspublicvoidDropDownControl(Controlcontrol){}publicvoidCloseDropDown(){}publicSystem.Windows.Forms.DialogResultShowDialog(Formdialog){returndialog.ShowDialog();}#endregion}#endregion#regionITypeDescriptorContext成员publicvoidOnComponentChanged(){}publicIContainerContainer{get{returnnull;}}publicboolOnComponentChanging(){返回真;//true保留更改,否则为false}publicobjectInstance{get{returnnull;}}publicPropertyDescriptorPropertyDescriptor{get{返回null;}}#endregion}由于我无法发表评论,我将其发布在这里:您可以通过在WindowsFormsEditorService.ShowDialog中的CollectionEditor的okButton上添加Click事件来获取DialogResultpublicSystem.Windows.Forms.DialogResultShowDialog(Formdialog){((系统对话框.Windows.Forms.Button)dialog.Controls.Find("okButton",true)[0]).Click+=WindowsFormsEditorService_Click;返回dialog.ShowDialog();}…以上就是C#学习教程:CollectionEditor有没有超凡的使用方法?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——privatevoidWindowsFormsEditorService_Click(objectsender,EventArgse){dr=DialogResult.OK;}本文收集自网络,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
