作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用可以让我更好地自定义 UI 的东西替换我的属性网格。我在我的表单上放置了一个按钮,我希望点击它时会弹出一个 CollectionEditor 并允许我修改我的代码。当我使用 PropertyGrid 时,我需要做的就是向指向我的 CollectionEditor 的属性添加一些属性并且它起作用了。但是如何手动调用 CollectionEditor?谢谢!
最佳答案
在这里找到答案:http://www.devnewsgroups.net/windowsforms/t11948-collectioneditor.aspx
以防万一上面链接的网站有一天会消失,这里是它的要点。但是,代码是从上面的链接中逐字记录的;评论是我的。
假设您有一个带有列表框和按钮的表单。如果您想使用 CollectionEditor 编辑 ListBox 中的项目,您可以在 EventHandler 中执行以下操作:
private void button1_Click(object sender, System.EventArgs e)
{
//listBox1 is the object containing the collection. Remember, if the collection
//belongs to the class you're editing, you can use this
//Items is the name of the property that is the collection you wish to edit.
PropertyDescriptor pd = TypeDescriptor.GetProperties(listBox1)["Items"];
UITypeEditor editor = (UITypeEditor)pd.GetEditor(typeof(UITypeEditor));
RuntimeServiceProvider serviceProvider = new RuntimeServiceProvider();
editor.EditValue(serviceProvider, serviceProvider, listBox1.Items);
}
现在您需要做的下一件事是创建 RuntimeServiceProvider()。这是上面链接中的发布者为实现此目的而编写的代码:
public class RuntimeServiceProvider : IServiceProvider, ITypeDescriptorContext
{
#region IServiceProvider Members
object IServiceProvider.GetService(Type serviceType)
{
if (serviceType == typeof(IWindowsFormsEditorService))
{
return new WindowsFormsEditorService();
}
return null;
}
class WindowsFormsEditorService : IWindowsFormsEditorService
{
#region IWindowsFormsEditorService Members
public void DropDownControl(Control control)
{
}
public void CloseDropDown()
{
}
public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
return dialog.ShowDialog();
}
#endregion
}
#endregion
#region ITypeDescriptorContext Members
public void OnComponentChanged()
{
}
public IContainer Container
{
get { return null; }
}
public bool OnComponentChanging()
{
return true; // true to keep changes, otherwise false
}
public object Instance
{
get { return null; }
}
public PropertyDescriptor PropertyDescriptor
{
get { return null; }
}
#endregion
}
关于c# - 有没有办法在属性网格之外使用 CollectionEditor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3816018/
我是一名优秀的程序员,十分优秀!