【问题标题】:How to implement a StringFormat in a property grid?如何在属性网格中实现 StringFormat?
【发布时间】:2014-12-09 22:07:54
【问题描述】:

场景

我对控件进行了子分类,我想在控件的属性网格中添加一个StringFormat 可扩展组,以便在设计时设置其值。

问题

我无法将 StringFormat 属性正确添加到属性网格中,这是我得到的:

问题

StringFormat 实现为PropertyGrid 的最简单方法是什么?

代码

我在这里尝试了@Plutonix 的建议:Create an expandable group in a property grid?(确定我做错了什么)

我宁愿避免使用TypeConverter,因为我不确定这种实现是否需要这种令人头疼的问题。

Public Class MyControl: Inherits ListBox

    Public Property MyProperty As MyStringFormat = New MyStringFormat

End Class

<TypeConverter(GetType(StringFormatConverter))>
Public Class MyStringFormat

    <Browsable(True)>
    <NotifyParentProperty(True)>
    <EditorBrowsable(EditorBrowsableState.Always)>
    Public Property myStringFormat As StringFormat

    Public Sub New()
        ' default values, if any
        myStringFormat = New StringFormat
    End Sub

End Class

Public Class StringFormatConverter : Inherits ExpandableObjectConverter

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext,
                                        culture As Globalization.CultureInfo,
                                        value As Object,
                                        destinationType As Type) As Object

        Return MyBase.ConvertTo(context, culture, value, destinationType)

    End Function

End Class

【问题讨论】:

  • 你能让你的例子不那么抽象吗?看起来你不需要一个可扩展的转换器来做一件事,而你的转换器什么也没做。我不确定是否需要它,因为道具网格的大多数转换器都是 ToString 和 FromString。我正在尝试弄清楚您到底在追求什么 - 字符串格式掩码或格式标志?
  • 感谢您的评论。我希望可能继承所需的类(或其他东西),我可以立即将组内 StringFormat 类的所有属性成员添加到网格中,而不需要更多的努力,因为如果我需要手动实现它,创建每个属性每一个一个地分开然后我已经可以自己做到了,我在问是否可以动态地做到这一点,但是如果一次将整个StringFormat 类属性实现到网格中比看起来更困难,那么这些是我更需要的道具吗:Alignment,FormatFlags,LineInputAlignment,Trimming
  • 好的,所以你可以把它变成一个包含这 4 个道具的扩展。没有为 StringFormat 定义 TypeConverter 或 UITypeEditor,因此您需要编写自己的类(除了它是一个密封类)。但由于 FormatFlags 是按位枚举,您可能必须为其实现 UITypeEditor。 (我假设 LineInputAlignment == LineAlignment)。
  • “expando”是这个意思吗?:msdn.microsoft.com/en-us/library/…,我以前从未见过,哈哈。
  • 无论是 TypeConverter 还是继承组件,因为您的 Type 将包含 4 个要编辑的内容。

标签: .net vb.net winforms class user-controls


【解决方案1】:

继续您之前的冒险,这就是您所需要的:

' property on the control

<Browsable(True), EditorBrowsable(EditorBrowsableState.Always),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
DefaultValue(-1)>
Public Property TextFormating As TextFormat

' be sure to instance it!
' TextFormating = New TextFormat

使用组件方法:

Public Class TextFormat
    Inherits Component

    <Browsable(True), NotifyParentProperty(True),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
    Public Property Alignment As StringAlignment

    <Browsable(True), NotifyParentProperty(True),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
    Public Property LineAlignent As StringAlignment

    <Browsable(True), NotifyParentProperty(True),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
    Public Property Trimming As StringTrimming

    <Browsable(True), NotifyParentProperty(True),
    Editor(GetType(UIEnumEditor), GetType(UITypeEditor)),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    EditorBrowsable(EditorBrowsableState.Always), DefaultValue(-1)>
    Public Property FormatFlags As StringFormatFlags

End Class

添加您想要的任何其他属性。你可以继承,因为它是一个密封的类,也没有你需要的所有属性。你不会喜欢StringFormatFlags 属性的默认行为;如果您认为 TypeConverter 令人头疼,那您就大饱眼福了!


由于StringFormatFlags 是一个标志枚举,您可能希望一次选择并组合多个。默认的 UITypeEditor 虽然是一个 DropDown,但我们想要一个 DropDownCheckList。为此,我们需要一个自定义 UITypeEditor。这些并不像听起来那么可怕,但也没有必要重新创建轮子:

CodeProject 上的Enhanced CollectionEditor Framework 文章是一个集合编辑器框架,其中还包括一个 EnumTypeEditor(滚动到文章末尾顺便提及的地方)。在文章的中间还有一个关于TypeConverters 的简短入门。

使用它:
- 下载文件并将 DLL 包含在您的项目中
- 然后(重新)装饰你的TextFormat.TextFormatFlags 属性:

<Editor(GetType(UIEnumEditor), GetType(UITypeEditor)),
Browsable(True), NotifyParentProperty(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always), DefaultValue(0)>
Public Property TextFormatFlags As StringFormatFlags

(注意添加的UIEnumEditor 属性)。

文章和演示展示了如何继承基础编辑器来自定义一些东西。它会做的一件事是使用Descriptions 而不是列表中的枚举名称,如果它们存在并且如果你愿意的话。就是这样,2个步骤,你可以检查要组合哪些标志:

你不需要一个 TypeConverter 因为将 4 个属性组合成一个字符串没有多大意义。和以前一样,当你的 Type 继承 Component 时,它仍然会有空/额外的下拉菜单。

【讨论】:

  • 抱歉我的下一个快速且可能无关紧要的问题,但如果你能给出一个简短的回答:我需要一个类型转换器从继承的类中完全删除摘要信息(在属性网格中看到)组件类?或者它只适用于expandoObject?
  • 以上没有总结因为我跳过了上一段中描述的TypeConverter。您将获得 properties 显示内容,因为它们必须显示它们所拥有的内容。
  • 是的.. 但我问它是因为您回答并提到了我的其他问题,然后,如果我想完全删除摘要信息而不是默认显示命名空间,您能否澄清一下我需要使用的是 TypeConverter,或者我可以使用属性标志?我今天意识到关于用户控件的问题太多了,如果你能在评论中澄清一下,谢谢,因为我的组件类在摘要信息中显示了命名空间,对我来说很难看。
  • 哦!你的意思是类型名称 - 让我看看其他答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多