gpt4 book ai didi

带参数的 C# UITypeEditor

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:51 25 4
gpt4 key购买 nike

我创建了一个自定义 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; }
}
}

最佳答案

EditValue方法有一个 context ITypeDescriptorContext 类型的参数并且有一个 Instance 属性,它是您正在编辑的属性的所有者对象。

所以你可以通过简单地转换 context.Instance 来访问你类中的任何公共(public)属性到所有者类类型。您也可以使用反射来访问私有(private)成员。

示例

这是一个接受 List<string> 的类在它的构造函数中,我将在编辑 SomeValue 时使用这些值属性(property)。为此,我将传递的列表存储在一个不可见的属性中 SomeList并将在 EditValue 中使用它我的自定义 UI 类型编辑器。如果愿意,您可以将列表保存在私有(private)属性中,然后使用反射提取值。这是一个简单的 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; }
}

这里是MyUITypeEditorEditValue方法,我从 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;

关于带参数的 C# UITypeEditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36577901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com