反射和自定义 ControlDesigner 似乎在 C# 中不起作用
我制作了一个自定义 ControlDesigner,我需要包含和排除属性网格中显示的属性。但由于某种原因,似乎只是忽略了代码?我不知道我可能做错了什么?我可能错过了什么吗?我需要设置VS之类的吗?
另外,在示例中,我发现他们似乎不同意删除调用应该在哪里。在某些示例中,他们在 preFilterProperties 方法中调用它,在某些示例中,他们在 postFilterProperties() 方法中调用它,这让我感到困惑。在某些示例中,他们在运行 base.preFilterProperties() 方法之后调用它,有时甚至在运行之前调用它?有人可以澄清一下吗?
这是到目前为止的代码。最好我还想添加一个带有将被排除的属性名称列表的属性,但我不知道何时设置这样的属性,因为反射似乎是在运行时之前运行的?或者有什么办法可以实现这一点吗?
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System;
using System.Collections;
using System.Collections.Generic;
namespace PropertyEditorProject
{
[Designer(typeof(YourClassDesigner))]
class MyPropertyClass
{
protected List<string> _MyList;
protected string _MyName;
protected string _MyCarModel;
protected int _MyAge;
public List<string> MyList
{
get
{
return _MyList;
}
set
{
_MyList = value;
}
}
public string MyName
{
get
{
return _MyName;
}
set
{
_MyName = value;
}
}
public string MyCarModel
{
get
{
return _MyCarModel;
}
set
{
_MyCarModel = value;
}
}
public int MyAge
{
get
{
return _MyAge;
}
set
{
_MyAge = value;
}
}
public MyPropertyClass()
{
_MyList = new List<string>();
_MyList.Add("Test");
_MyList.Add("Testing 2");
_MyName = "TestName";
_MyCarModel = "Subaru";
_MyAge = 20;
}
}
internal class YourClassDesigner : ControlDesigner
{
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
properties.Remove("MyAge");
base.PreFilterProperties(properties);
}
}
static class MainProgram
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Form form = new Form();
PropertyGrid grid = new PropertyGrid();
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
grid.SelectedObject = new MyPropertyClass();
Application.Run(form);
}
}
}
有人有什么想法吗?感谢任何帮助
I have made a custom ControlDesigner that I need to include and exclude properties shown in the property grid. But for some reason it seems just to ignore the code? I don't know what I might have done wrong? Could I be missing something? Do I need to setup VS or something?
Also in the examples I have found they seem to disagree about where the remove call should be. In some examples they call it in preFilterProperties method and in some examples they call it in postFilterProperties() method which confuses me. In some examples they call it after running the base.preFilterProperties() method and sometimes before? Can someone please clarify?
Here is the code so far. Preferrably I would also like to add a property with a list of property names that would be excluded but I don't know when to set such a property since it seems that reflection is run before runtime? Or is there any way to accomplish that?
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System;
using System.Collections;
using System.Collections.Generic;
namespace PropertyEditorProject
{
[Designer(typeof(YourClassDesigner))]
class MyPropertyClass
{
protected List<string> _MyList;
protected string _MyName;
protected string _MyCarModel;
protected int _MyAge;
public List<string> MyList
{
get
{
return _MyList;
}
set
{
_MyList = value;
}
}
public string MyName
{
get
{
return _MyName;
}
set
{
_MyName = value;
}
}
public string MyCarModel
{
get
{
return _MyCarModel;
}
set
{
_MyCarModel = value;
}
}
public int MyAge
{
get
{
return _MyAge;
}
set
{
_MyAge = value;
}
}
public MyPropertyClass()
{
_MyList = new List<string>();
_MyList.Add("Test");
_MyList.Add("Testing 2");
_MyName = "TestName";
_MyCarModel = "Subaru";
_MyAge = 20;
}
}
internal class YourClassDesigner : ControlDesigner
{
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
properties.Remove("MyAge");
base.PreFilterProperties(properties);
}
}
static class MainProgram
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Form form = new Form();
PropertyGrid grid = new PropertyGrid();
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
grid.SelectedObject = new MyPropertyClass();
Application.Run(form);
}
}
}
Anyone have any ideas? Grateful for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番搜索后,我找到了一个问题的答案:
“遵循的一般规则是在“PreFilter”方法中添加或删除项目,并修改“PostFilter”方法中的现有项目。始终调用基本方法首先在 PreFilter 方法中调用,最后在 PostFilter 方法中调用基方法,这确保了所有设计器类都有适当的机会来应用其更改。”
http://msdn.microsoft.com/en-us/library/ms973820.aspx
但我还是不明白为什么代码不起作用:(
After a bit of searching I found the answer to one of my questions:
"The general rule to follow is to add or remove items in the "PreFilter" methods, and modify existing items in the "PostFilter" methods. Always call the base method first in the PreFilter methods and call the base method last in the PostFilter methods. This ensures that all designer classes are given the proper opportunity to apply their changes."
http://msdn.microsoft.com/en-us/library/ms973820.aspx
Still I can't figure out why the code won't work though :(