开发者

UserControl custom property grayed

开发者 https://www.devze.com 2023-04-07 22:18 出处:网络
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 ha

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.

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:

UserControl custom property grayed

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. The .NET framework provides the ExpandableObjectConverter class to make this easier.

For example:

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

You need to follow these steps to create a custom TypeConverter.

To implement a simple type converter that can translate a string to a Point

  1. Define a class that derives from ExpandableObjectConverter (or TypeConverter).
  2. Override the CanConvertFrom method that specifies which type the converter can convert from. This method is overloaded.
  3. Override the ConvertFrom method that implements the conversion. This method is overloaded.
  4. Override the CanConvertTo method that specifies which type the converter can convert to. 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. This method is overloaded.
  6. Override the IsValid method that performs validation. This method is overloaded.

Take a look at the following MSDN page for more information on how to implement a TypeConverter:

How to: Implement a Type Converter

After you've created the TypeConverter you can apply it to your custom type.

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

And all will be well:

UserControl custom property grayed

I quickly summarized an elobarate example from MSDN. You can find an entire walkthrough here:

Getting the Most Out of the .NET Framework PropertyGrid Control

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号