我已经创建了一个自定义UITypeEditor,它启动一个表单(StringSelector)来显示用户选择的字符串列表。问题是,这个表单需要知道要使用什么StringManager (stringmanage只是一个包含列表中允许的所有字符串的类)。
在创建此表单时,我将StringManager作为构造函数中的一个参数传递,但我无法确定如何使用UITypeEditor完成此操作。
下面是我的当前代码,它使用没有参数的构造函数,只是为了让表单显示出来,但是显然没有字符串,因为我没有调用构造函数的参数版本。
如何将参数传递给UITypeEditor,然后在EditValue函数中使用该参数?非常感谢。
class StringSelectorEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
StringItem item = value as StringItem;
if (svc != null)
{
// ###### How do I pass a reference to this EditValue function so that I can....
using (StringSelector form = new StringSelector(/* ... INSERT IT HERE */))
{
form.Value = item;
if (svc.ShowDialog(form) == DialogResult.OK)
{
item = form.Value; // update object
}
}
}
return value; // can also replace the wrapper object here
}
}根据请求更新了:,我有一个名为ControlInstance的类,它本身包含已填充的StringManager。正是这个ControlInstance类被传递给PropertyGrid控件及其显示在其中的访问器函数,包括上面描述的StringSelectorEditor UITypeEditor引用。下面是代码的一个片段:
public class ControlInstance_Label : ControlInstance
{
StringManager stringManager;
string thisName = "";
StringItem linkedStringItem;
public ControlInstance_Label(String TextFilePath)
{
// Code here which populates the StringManager with text from the above file
}
[Category("Design"), Description("Control Name")]
public String Name
{
get { return thisName; }
set { thisName = value; }
}
// THIS IS WERE I SOMEHOW NEED TO PASS IN THE StringManager Ref to the EditValue function of the custom UITypeEditor
[Category("Design"), Description("Static String Linked to this Control")]
[Editor(typeof(StringSelectorEditor), typeof(UITypeEditor))]
public StringItem LinkedString
{
get { return linkedStringItem; }
set { linkedStringItem = value; }
}
}发布于 2017-09-09 17:25:04
EditValue方法具有一个context参数,该参数类型为ITypeDescriptorContext,并具有一个Instance属性,该属性是您正在编辑的属性的所有者对象。
因此,您可以通过简单地将context.Instance转换为所有者类类型来访问类中的任何公共属性。此外,您也可以使用反射来访问私有成员。
示例
下面是一个类,它在构造函数中接受List<string>,我将在编辑SomeValue属性时使用这些值。为此,我将传递的列表存储在不可见属性SomeList中,并将在我的自定义UI类型编辑器的EditValue中使用它。如果愿意,可以将列表保留在私有属性中,然后使用反射提取值。下面是一个简单的MyClass实现:
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; }
}在MyUITypeEditor in EditValue方法中,我从context.Instance中提取列表值,这是我们正在编辑其属性的对象的实例。然后,例如,我在消息框中显示提取的值:
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;
}
}要测试它,只需在属性网格中显示MyClass的一个实例,并尝试在属性网格中编辑SomeValue属性:
var myObject = new MyClass(new List<string>() { "A", "B", "C" });
this.propertyGrid1.SelectedObject = myObject;https://stackoverflow.com/questions/36577901
复制相似问题