创建具有可浏览、可扩展字段的属性 [英] Creating a Property with Browsable, Expandable Fields

查看:26
本文介绍了创建具有可浏览、可扩展字段的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了阐明我的要求,请查看设计器中的 Font 属性.当您单击其下拉菜单时,您将获得 ForeColor、ImeMode、Language 等选项.

To clarify what I am asking, look at the Font property in the designer. When you click its dropdown, you get the options ForeColor, ImeMode, Language, etc.

我想要相同类型的下拉列表,允许我为我自己添加到设计器的属性输入多个值(它将用于我的自定义控件中的数据库属性,并为您提供选择版本、类型的选项, 等等.).

I want the same kind of dropdown that allows me to enter in several values for a property I am adding to the designer myself (It will be for a database property in my custom control and give you an option to select Version, Type, etc.).

我该怎么做?找遍了也没找到解决办法.

How would I go about doing this? I've looked all over and I can't find a solution.

谢谢.

推荐答案

您正在寻找的内容由 TypeConverter 提供.具体来说,您需要一个 ExpandableObjectConverter - 这会打破"子属性.我的假类型 - 它是您控制的替代品:

What you are looking for is provided by a TypeConverter. Specifically, you need an ExpandableObjectConverter - this "breaks out" the child properties. My fake type - it is a stand-in for your control:

Public Class Widget
    Public Property Name As String
    Public Property FooValue As Integer

    Public Property XYOffset As Offset
     ...

Offset 本身就是一个由三个属性组成的类型:

Offset is itself a Type made up of three properties:

Public Class Offset
    Public Property Name As String
    Public Property X As Int32
    Public Property Y As Int32
    ...
    Public Overrides Function ToString() As String
        Return String.Format("{0}: ({1}, {2})", Name, X.ToString, Y.ToString)
    End Function

如果不清楚,Widget 将是您的控件,XYOffset 将是您要询问的属性.ToString() 覆盖使您不会在属性网格中获得丑陋的类型名称 (WindowsApplication14.SomeType).PropertyGrid 控件中的结果:

In case it is not clear, Widget would be your control, XYOffset would be the property you are asking about. The ToString() overrides is so that you dont get the ugly Type name (WindowsApplication14.SomeType) in property grids. The result in a PropertyGrid control:

它被禁用,因为 NET 不知道如何编辑 Offset 对象.这只需要最简单的 TypeConverters 之一:

It is disabled and because NET doesnt know how to edit an Offset object. This just requires one of the simplest TypeConverters:

Public Class XYOffsetConverter
    Inherits ExpandableObjectConverter

    ' more to come 
End Class

装饰物业:

Public Class Offset
     ...
    <TypeConverter(GetType(XYOffsetConverter))>
    Public Property XYOffset As Offset

Offset 属性可以使用: 但由于您提到这是一个 CustomControl,您将需要其他功能TypeConverter 稍后(见注释).结果是几乎没有工作的核心功能:

The Offset property could use: <TypeConverter(GetType(ExpandableObjectConverter))> but since you mentioned this is a CustomControl, you will need other functionality in your TypeConverter later (see Notes). The result is the core functionality with almost no work:

由于 NET 确实知道如何编辑 StringInt32 编辑工作在打开的属性上.它缺少两件事:
a) 当您编辑子属性时,Offset 属性摘要"不会更新(X 在图像中不匹配)
b) 用户无法编辑顶部的摘要"字符串来更改属性.

Since NET does know how to edit String and Int32 the edits work on the opened up properties. There are 2 things lacking in it:
a) As you edit the child properties, the Offset Property "summary" doesnt update (X doesnt match in the image)
b) The user cant edit the top 'summary' string to change the properties.

修复第一个很容易.只需向 Offset 属性添加一个属性:

Fixing the first is easy. Just add an attribute to the Offset properties:

Public Class Offset
    <NotifyParentProperty(True)>
    Public Property Name As String
    <NotifyParentProperty(True)>
    Public Property X As Int32
    <NotifyParentProperty(True)>
    Public Property Y As Int32

现在,每次编辑子属性后都会更新摘要:

Now, the summary updates after each child property edit:

修复 B 并不难 - 您只需要解析您在 ToString() 中创建的字符串并从中返回一个新的偏移量.然而,这不是必需的,只是很好,因为他们可以打开它并编辑每个值.

Fixing B is not hard - you just have to parse the string you create in ToString() and return a new Offset from it. However it is not necessary, just nice, since they can open it and edit each value.

另一件事是它取决于那些东西是什么,我不想猜测你的类型(类)是什么样的.大多数 TypeConverters 是特定于类型的,我不知道你的是什么样的.

The other thing is that it depends on what those things are and I dont want to guess what your Type (Class) looks like. Most TypeConverters are type-specific and I have no idea what yours looks like.

我在示例中使用了属性网格,但控件在 VS 属性窗口中的工作方式相同.

I used a property grid for the examples, but a control would work the same in the VS property window.

注意
您指出这是用于自定义控件.在这种情况下,您将需要帮助 VS 序列化您的类型,因为它不知道如何创建在设计器中创建的 Offset 类型.这是您的 TypeConverter 的另一项工作.我只是不知道你长什么样.

Note
You indicated this was for a custom control. In that case, you will need to help VS serialize your Type because it will not know how to create an Offset Type created in the designer. This is another job for your TypeConverter. I just dont know what your looks like.

这篇关于创建具有可浏览、可扩展字段的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆