简体   繁体   English

UserControl自定义属性为灰色

[英]UserControl custom property grayed

I'm quite sure this is a stupid question, but all my tries failed. 我很确定这是一个愚蠢的问题,但我所有的尝试都失败了。
I have a custom control in which I'd like to have a complex property exposing many properties. 我有一个自定义控件,我希望有一个复杂的属性暴露许多属性。 I want to do this because I would have an expandable property in visual property manager, so I can set sub-properties easily because grouped together in parent property. 我想这样做是因为我在可视属性管理器中有一个可扩展的属性,所以我可以很容易地设置子属性,因为它们在父属性中组合在一起。 This is a schema of what I've done: 这是我所做的模式:

public partial class QViewer : UserControl
{
    private Shortcuts toolsShortcuts = new Shortcuts();
    private TestProp testProp = new TestProp();

    public Shortcuts ToolsShortcuts { get { return toolsShortcuts; } }
    public TestProp Test { get { return testProp; } }
}


public struct TestProp
{
    public bool DoIt;
    public DateTime Date;
}

public class Shortcuts
{
    Keys toolArrow = Keys.None;
    public Keys Arrow
    {
        get { return toolArrow; }
        set { ... }
    }
}

} }

When I insert my custom control in a form (using another project inside same solution) and I open properties, both Shortcuts and Test are grayed, not expandable, so I can't set properties inside them. 当我在表单中插入自定义控件(使用同一解决方案中的另一个项目)并打开属性时, 快捷方式测试都是灰色的,不可扩展,因此我无法在其中设置属性。
What's wrong? 怎么了? Is there a better way to group properties than creating a class or a struct? 有没有比创建类或结构更好的方法来分组属性?
Thanks to everybody! 谢谢大家!

IIRC you need to write a TypeConverter to get the properties window to expand these properties. IIRC你需要编写一个TypeConverter来获取属性窗口来扩展这些属性。

Let's assume you use the following type for a complex property: 假设您对复杂属性使用以下类型:

[DescriptionAttribute("Expand to see the spelling options for the application.")]
public class SpellingOptions
{
    private bool spellCheckWhileTyping = true;
    private bool spellCheckCAPS = false;
    private bool suggestCorrections = true;

    [DefaultValueAttribute(true)]
    public bool SpellCheckWhileTyping 
    {
        get { return spellCheckWhileTyping; }
        set { spellCheckWhileTyping = value; }
    }

    [DefaultValueAttribute(false)]
    public bool SpellCheckCAPS 
    {
        get { return spellCheckCAPS; }
        set { spellCheckCAPS = value; }
    }

    [DefaultValueAttribute(true)]
    public bool SuggestCorrections 
    {
        get { return suggestCorrections; }
        set { suggestCorrections = value; }
    }
}

Your properties probably look like this at the moment: 您的属性目前可能看起来像这样:

在此输入图像描述

Notice that the Spell Check Options property is grayed out. 请注意,“拼写检查选项”属性显示为灰色。

You need to create a TypeConverter to convert your object type so it can be displayed correctly. 您需要创建一个TypeConverter来转换您的对象类型,以便它可以正确显示。 The .NET framework provides the ExpandableObjectConverter class to make this easier. .NET框架提供了ExpandableObjectConverter类以使其更容易。

For example: 例如:

public class SpellingOptionsConverter:ExpandableObjectConverter 
{
    //...
}

You need to follow these steps to create a custom TypeConverter. 您需要按照以下步骤创建自定义TypeConverter。

To implement a simple type converter that can translate a string to a Point 实现一个简单的类型转换器,可以将字符串转换为Point

  1. Define a class that derives from ExpandableObjectConverter (or TypeConverter). 定义一个派生自ExpandableObjectConverter(或TypeConverter)的类。
  2. Override the CanConvertFrom method that specifies which type the converter can convert from. 覆盖CanConvertFrom方法,该方法指定转换器可以转换的类型。 This method is overloaded. 此方法已重载。
  3. Override the ConvertFrom method that implements the conversion. 覆盖实现转换的ConvertFrom方法。 This method is overloaded. 此方法已重载。
  4. Override the CanConvertTo method that specifies which type the converter can convert to. 覆盖CanConvertTo方法,该方法指定转换器可以转换为哪种类型。 It is not necessary to override this method for conversion to a string type. 没有必要重写此方法以转换为字符串类型。 This method is overloaded. 此方法已重载。
  5. Override the ConvertTo method that implements the conversion. 覆盖实现转换的ConvertTo方法。 This method is overloaded. 此方法已重载。
  6. Override the IsValid method that performs validation. 覆盖执行验证的IsValid方法。 This method is overloaded. 此方法已重载。

Take a look at the following MSDN page for more information on how to implement a TypeConverter: 有关如何实现TypeConverter的更多信息,请查看以下MSDN页面:

How to: Implement a Type Converter 如何:实现类型转换器

After you've created the TypeConverter you can apply it to your custom type. 创建TypeConverter后,您可以将其应用于自定义类型。

[TypeConverterAttribute(typeof(SpellingOptionsConverter)),
 DescriptionAttribute("Expand to see the spelling options for the application.")]
public class SpellingOptions{ ... }

And all will be well: 一切都会很好:

在此输入图像描述

I quickly summarized an elobarate example from MSDN. 我很快总结了一个来自MSDN的elobarate示例。 You can find an entire walkthrough here: 您可以在此处找到完整的演练:

Getting the Most Out of the .NET Framework PropertyGrid Control 充分利用.NET Framework PropertyGrid控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM