EditValue
method has a context
parameter which is of type ITypeDescriptorContext
and has an Instance
property which is the owner object of the property that you are editing.
So you can have access to any public properties in your class by simply casting context.Instance
to the owner class type. Also you can use reflection to have access to private members as well.
Example
Here is a class that accepts a List<string>
in its constructor and I'm going to use those values when editing SomeValue
property. To do so, I store the passed list in an invisible property SomeList
and will use it in EditValue
of my custom UI type editor. You can keep the list in a private property if you like and then extract values using reflection. Here is a simple MyClass
implementation:
public class MyClass
{
[Browsable(false)]
public List<String> SomeList { get; set; }
public MyClass(List<String> list)
{
SomeList = list;
}
[Editor(typeof(MyUITypeEditor), typeof(UITypeEditor))]
public string SomeValue { get; set; }
}
Here in MyUITypeEditor
in EditValue
method, I extract list values from context.Instance
which is the instance of the object which we are editing its property. Then just for example I show extracted values in a message box:
public class MyUITypeEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
//Just as an example, show a message box containing values from owner object
var list = ((MyClass)context.Instance).SomeList;
MessageBox.Show(string.Format("You can choose from {0}.",
string.Join(",", list)));
return base.EditValue(context, provider, value);
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
To test it, it's enough to show an instance of MyClass
in a property grid and try to edit SomeValue
property in property grid:
var myObject = new MyClass(new List<string>() { "A", "B", "C" });
this.propertyGrid1.SelectedObject = myObject;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…