[英]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
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.