C# 如何在VisualStudio的“属性”窗口中使类型可编辑?

C# 如何在VisualStudio的“属性”窗口中使类型可编辑?,c#,visual-studio,C#,Visual Studio,例如,我们有这些类型 public struct Vector2D { public double X{get; set;} public double Y{get; set;} } 我的一个用户控件的属性名为Value,类型为Vector2D 目前,如果我查找该属性。它将显示.Vector2D,不可编辑。(请注意,该属性是可编辑的,而不是按字面意思显示) 如何通过Visual Studio中的“属性”窗口使该属性可编辑,如点、大小、填充等 尝试在没有参数的情况下添加Brows

例如,我们有这些类型

public struct Vector2D
{
    public double X{get; set;}
    public double Y{get; set;}
}
我的一个用户控件的属性名为
Value
,类型为
Vector2D

目前,如果我查找该属性。它将显示
.Vector2D
,不可编辑。(请注意,该属性是可编辑的,而不是按字面意思显示)

如何通过Visual Studio中的“属性”窗口使该属性可编辑,如
大小
填充


尝试在没有参数的情况下添加
BrowsableAttribute
EditorAttribute
,但不起作用

我认为您的意思是通过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
    }

我希望这能解决您的问题

抱歉,但是VS不是
Unity
或其他东西,您必须通过代码手动初始化/分配属性(如果我正确理解了您的问题)。@SeM该类型是控件属性之一的类型。如果该属性的类型为
int
string
,则该属性是可编辑的。这可能是因为您的属性不是公共属性。在它的前面添加public。我还建议重写ToString函数,它是您选择的一种格式。如果这些都不起作用,我有一种方法可以在属性网格中自定义编辑您的值。但这只有在结构本身是另一个对象的属性时才有效class@DaanV它在实际代码中是公共的,不过。我会发布我的propertygrid的自定义编辑器代码,然后Struct无法继承类。TypeConfig和EditTypeConfig在哪里?哦,对不起,TypeConfig是我实现该UITypeEditor的原始类,将其替换为实现UiTypeEditor的类,EditTypeConfig是一个Windows窗体,允许您自定义生成可以编辑相关对象的内容,例如属性网格控件中大多数集合编辑器的tabcontrol editor