我为一个房产创建了一个编辑器.但是我想将一些参数传递给编辑器的构造函数,但我不确定如何执行此操作.
FOO _foo = new foo();
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public object foo
{
get { return _foo; }
set {_foo = value;}
}
Run Code Online (Sandbox Code Playgroud)
〜
class MyEditor: UITypeEditor
{
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
//some other code
return obj;
}
}
Run Code Online (Sandbox Code Playgroud)
Szy*_*bki 11
我知道这是一个老问题,但我遇到了类似的问题,唯一提供的答案并没有解决它.
所以我决定编写自己的解决方案,这有点棘手,更像是一种解决方法,但它肯定对我有用,也许会帮助别人.
这是它的工作原理.由于您没有创建自己的UITypeEditor派生类的实例,因此您无法控制传递给构造函数的参数.您可以做的是创建另一个属性并将其分配给同一属性,您将分配自己的UITypeEditor并将您的参数传递给该属性,然后从该属性中读取值.
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
[MyEditor.Arguments("Argument 1 value", "Argument 2 value")]
public object Foo { get; set; }
class MyEditor : UITypeEditor
{
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
string property1 = string.Empty, property2 = string.Empty;
//Get attributes with your arguments. There should be one such attribute.
var propertyAttributes = context.PropertyDescriptor.Attributes.OfType<ArgumentsAttribute>();
if (propertyAttributes.Count() > 0)
{
var argumentsAttribute = propertyAttributes.First();
property1 = argumentsAttribute.Property1;
property2 = argumentsAttribute.Property2;
}
//Do something with your properties...
return obj;
}
public class ArgumentsAttribute : Attribute
{
public string Property1 { get; private set; }
public string Property2 { get; private set; }
public ArgumentsAttribute(string prop1, string prop2)
{
Property1 = prop1;
Property2 = prop2;
}
}
}
Run Code Online (Sandbox Code Playgroud)