- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个自定义 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; }
}
这里是MyUITypeEditor
在 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;
关于带参数的 C# UITypeEditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36577901/
我有一个自定义的 UITypeEditor,用于使用 propertygrid 与我的程序进行颜色选择,但如果我只公开 system.drawing.color,我似乎无法激活它。我需要用 Custo
我创建了一个自定义 UITypeEditor,它启动一个表单 (StringSelector) 以显示用户从中选择的字符串列表。问题是这个表单需要知道要使用什么 StringManager(strin
我目前希望使用 PropertyGrid 来允许用户编辑我的一些类,但是我在将对象传递给他们使用的 UITypeEditor 时遇到了困难。当用户按下下拉菜单时,我想显示一个包含已加载纹理的列表框以供
我有一个扩展器(IExtenderProvider),它扩展了某些类型的 具有附加属性的控件。对于这些属性之一,我有 写了一个 UITypeEditor。到目前为止,一切正常。 扩展器本身也有几个属性
我有一个属性网格,其中一个属性使用 UITypeEditor编辑值(在表单上)。 但是该属性仍然是可编辑的,这是我不想要的。有没有办法做到这一点?我看了这个类似的问题Propertygrid UIEd
我为一个属性创建了一个编辑器。但是,我想将一些参数传递给编辑器的构造函数,但我不确定如何执行此操作。 FOO _foo = new foo(); [Editor(typeof(MyEditor), t
我有一个属性网格,当单击其中一个属性的按钮时,某些字段会被填充。但是属性的集合不会被触发。我不知道为什么。 private OptoSigmaSettings dataToGet = new Opt
我正在尝试编写一个简单的属性网格,以允许用户修改图表的颜色。默认情况下,图表具有“Palette”属性,该属性的枚举类型为“ChartColorPalette”。如果作为我的属性网格基础的对象也具有相
我有一个自定义对象,可以通过自定义 TypeEditor(谈论 .NET、c# 和 winforms)从 PropertyGrid (DevExpress) 进行编辑。 我的自定义 UITypeEdi
我创建了一个自定义 UITypeEditor。 我能否从 UITypeEditor 类内部插入一个属性,该属性也将 TypeConverter 附加到我的属性? 我已经尝试了以下方法,但无论我如何扭曲
我想避免在我为其编写自定义 UITypeEditor 的特定类型的每个实例上放置 EditorAttribute。 我无法在类型上放置 EditorAttribute,因为我无法修改源。 我有一个将要
根据 http://msdn.microsoft.com/en-us/library/system.drawing.design.uitypeeditoreditstyle.aspx,我有一个 Win
我想知道 .Net-3.5 是否带有内置 List或 string[] TypeConverter或 UITypeEditor这样我就可以从属性网格编辑这种属性。 最佳答案 UITypeEditor
我正在调整 WinForms 应用程序。此应用程序有一个包含 PropertyGrid 的 Form。一个对象被分配给 SelectedObject 属性,以便属性网格显示该对象的属性。 分配的对象类
我试图全面了解您如何使用 ICustomTypeDescriptor、TypeDescriptionProvider、TypeConverter 和 UITypeEditor 来更改 Property
我正忙于编写一个 CodeSmith 模板,它的属性之一是类型 System.Type .我希望能够使用 UI 选择程序集,加载程序集,然后显示该程序集中可用的类型。然后我可以去选择其中一种。 有没有
我有一个具有一些属性的类。更改其中一个后,我想使用react并更改另一个属性。此操作应由 UITypeEditor 或 TypeConverter 完成,而不是由类本身的事件完成。 我不想使用 INo
在 Visual Studio 中,当您在解决方案资源管理器中选择项目或项目项时,有时您可能希望将自定义属性添加到属性窗口(按 F4 时弹出的窗口)。此外,要填写这些属性的值,我需要添加一个按钮来弹出
这是一个令人尴尬的问题,但我觉得我在过去几个小时里尝试了一切。 我只想在我的属性中添加以下属性 #using #using ... using namespace System::Draw
我是一名优秀的程序员,十分优秀!