例如,我们有这些类型
public struct Vector2D
{
public double X{get; set;}
public double Y{get; set;}
}
我的一个用户控件具有名为Value
的属性,类型为Vector2D
当前,如果我查找该属性。它将显示<namespace>.Vector2D
,并且不可编辑。 (请注意,该属性是可编辑的,实际上并没有那样显示)
如何通过Visual Studio中的“属性”窗口使该属性可编辑,就像Point
,Size
,Padding
等一样?
尝试添加不带参数的BrowsableAttribute
,EditorAttribute
,但不起作用。
答案 0 :(得分:0)
我认为您的意思是通过PropertyGrid控件编辑变量。如果是这样,您可以调用自定义属性编辑器来编辑该特定对象。我还建议覆盖您选择的ToString函数。 PropertyGrid使用该值来填充值以显示给用户。
如何实现自定义编辑器:
public struct Vector2D : UITypeEditor
{
//UITypeEditor Implementation
//Gets the editor style, (dropdown, value or new window)
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
//Gets called when the value has to be edited.
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
//Calls a dialog to edit the object in
EditTypeConfig editor = new EditTypeConfig((TypeConfig)value);
editor.Text = context.PropertyDescriptor.Name;
if (editor.ShowDialog() == DialogResult.OK)
return editor.SelectedObject;
else
return (TypeConfig)value;
}
//Properties
[DisplayName("X"), Description("X is something"), Category("Value"), ReadOnly(false)]
public double X { get; set; }
[DisplayName("Y"), Description("Y is something"), Category("Value"), ReadOnly(false)]
public double Y { get; set; }
}
用法:
public class Sample
{
[DisplayName("Value"), Description("Value is something"), Category("Vectors"), ReadOnly(false)]
[Editor(typeof(Vector2D), typeof(UITypeEditor))]
public Vector2D Value { get; set; }
//Editor(typeof(Vector2D)) calls a class that handles the the editing of that value
}
我希望这可以解决您的问题