设计时编辑器对控件集合的支持 [英] Design-time editor support for controls collection

查看:102
本文介绍了设计时编辑器对控件集合的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向组件添加一个表示控件集合的属性,并具有一个集合编辑器,通过它可以轻松地选择属于该集合的控件。 VS使用以下代码自动执行 我想要的操作:

I'd like to add a property which represents a collection of controls to a component and have a collection editor with which I can easily select the controls that belong to the collection. VS does almost what I want automatically with the following code:

    Private _controls As New List(Of Control)
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property SelectedControls() As List(Of Control)
        Get
            Return _controls
        End Get
    End Property

我得到默认的CollectionEditor,可以添加和删除控件,并且集合被序列化。

I get the default CollectionEditor, can add and remove controls, and the collection is serialized.

问题是我不想添加 new 控件,我想从表单上的其他可用控件中选择多个控件。有什么简单/标准的方法可以做到这一点,还是我必须编写自己的集合编辑器并运行控件集合?

The problem is that I don't want to add new controls, I want to select multiple controls from the other available controls on the form. Is there any easy/standard way to do this, or will I have to write my own collection editor and run through the controls collection?

尽管关于集合编辑器(UITypeEditors)和其他设计时主题的材料很多,但我找不到任何链接来证明这种确切的行为,因此

Although there's plenty of material on collection editors (UITypeEditors) and other design-time topics, I haven't been able to find any links demonstrating this exact behaviour, so any help is appreciated.

推荐答案

确定,因此,在此期间,我吃了午饭并亲自编写了编辑器。并不是很大的努力。如果有帮助,我很乐意分享。

OK, so in the mean time I've had lunch and written the editor myself. Wasn't such a great effort. I'd be glad to share, if it helps.

编辑:解决方案摘要

我编写了一个编辑器(请参阅屏幕截图),该编辑器以树形视图的形式递归列出了在窗体上找到的所有控件。如果控件具有文本或图像属性,则显示文本/图像。用户可以选择带有复选框的多个控件,甚至可以过滤列表以仅显示某些类型的控件。

I wrote an editor (see screenshot) which recursively lists all the controls found on the form in a treeview. If the controls have their Text or Image properties set the text/image is displayed. The user can select multiple controls with checkboxes and even filter the list to only display controls of certain types.

(不过,我必须承认,因为该编辑器仅用于供内部使用,我不必费心检查一般的image属性,我只处理几个众所周知的控件类型。)

(I must admit, though, that since this editor was only intended for internal use, I didn't bother to check for the image property generically, I just handle a couple of well-known control types.)


编辑器表单中的代码实际上仅用于UI ,负责填充树,设置对预选控件列表的检查,并在用户单击确定关闭表单时返回所选控件的列表。

The code in the editor form is really just for the UI, responsible for filling the tree, setting the checks on the list of pre-selected controls and returning the list of selected controls when the user closes the form with OK.

下一步,我们有一个实现UITypeEditor的类,我称之为FormControlSelectionEditor。此类分配给我们要使用编辑器以使用[Editor]属性的属性。它只需要在需要时创建表单的新实例并将其显示为模式对话框即可。

Next, we have a class that implements UITypeEditor, which I called FormControlSelectionEditor. This class is assigned to the properties which we want to use the editor for using the attribute. It doesn't do much more than create a new instance of the form when required and display it as a modal dialog.

然后是属性本身,它们是类型System.Collections.ObjectModel.ObservableCollection(Of Control)。我之所以选择ObservableCollection,是因为我还需要在运行时对列表进行更改,但是其他列表在进行较小的调整后也可以做到。

Then there are the properties themselves, which are of type System.Collections.ObjectModel.ObservableCollection(Of Control). I chose ObservableCollection because I need to react to changes to the lists at run-time as well, but other lists would do just as well with minor adaptation.

发现是我必须编写我的属性和编辑器,以便它们使用控件列表的副本。换句话说,UITypeEditor代码复制存储在属性中的列表,并将其传递到编辑器表单(用于在打开表单时设置检查),并在关闭表单时清除属性的后备列表,并复制从编辑器返回的列表中的每个控件。我发现否则,.designer文件中的序列化有问题。我不认为一定是这种方式;我认为这很可能是我的错误。

One thing I discovered is that I had to write my properties and editor such that they use copies of the lists of controls. In other words, the UITypeEditor code makes a copy of the list stored in the property and passes it to the editor form (for setting the checks when the form is opened), and when the form is closed, I clear the property's backing list and copy over each control in the list returned from the editor. I found that otherwise I had problems with serialization in the .designer file. I don't believe it has to be this way; I think it's more likely an error on my part.

典型属性的代码:

    Private WithEvents _insertButtons As New System.Collections.ObjectModel.ObservableCollection(Of Control)
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    <Editor(GetType(Design.FormControlSelectionEditor), GetType(UITypeEditor))> _
    Public Property InsertButtons() As System.Collections.ObjectModel.ObservableCollection(Of Control)
        Get
            Return _insertButtons
        End Get
        Set(ByVal value As System.Collections.ObjectModel.ObservableCollection(Of Control))
            If value Is Nothing Then
                RemoveInsertButtonEventHandlers(_insertButtons)
                _insertButtons.Clear()
            Else
                ' Copy the list items passed in into the internal list
                For i As Integer = _insertButtons.Count - 1 To 0 Step -1
                    If Not value.Contains(_insertButtons.Item(i)) Then _insertButtons.RemoveAt(i)
                Next
                For Each ctl As Control In value
                    If Not _insertButtons.Contains(ctl) Then _insertButtons.Add(ctl)
                Next
            End If
        End Set
    End Property
    Private Function ShouldSerializeInsertButtons() As Boolean
        Return _insertButtons.Count > 0
    End Function
    Private Sub ResetInsertButtons()
        InsertButtons = Nothing
    End Sub

我已将编辑器放入一个zip文件中; 在此处下载

I've put the editor into a zip file; download it here.

正如我之前提到的,这只是一个快速的&脏溶液仅供内部使用。一直以来,我都希望有任何改进的建议。

As I mentioned before, this was just a quick & dirty solution intended for internal use only. All the same, I'd appreciate any suggestions for improvement.

这篇关于设计时编辑器对控件集合的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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