6

我已经创建了我的自定义控件。它有一个名为“Tab”的属性。此属性将从“DockContainerItem”类继承的“FloorsInformation”控件的集合添加到我的自定义控件中。

我的自定义控件属性

现在,我想在单击选项卡“CollectionEditor”窗口的“确定”按钮后将“FloorsInformation”控件添加到我的自定义控件中。

我有这样做的“AddTabs”方法。但是,我不能在正确的地方调用它。我必须在“Tab”属性的“set accessor”中调用“AddTabs”方法,但它永远不会发生。

我也可以从“Tab”属性的“get accessor”中调用这个方法,但是在“Tab”属性的“get accessor”中调用这个方法会报错,因为程序访问“get accessor”的“Tab”属性连续。

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
[ToolboxItem(true), ToolboxBitmap(typeof(ToolboxIconResourceFinder), "FloorsGrouping.bmp")]
[DisplayName("Floors Group")]
[Editor("WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc", typeof(UITypeEditor))]
[Description("Floorssssssss")]
[Category("Saino")]
[DefaultProperty("Text")]
[DesignerCategory("Component")] //Form //Designer //Empty String ("")
public partial class FloorsGrouping : Bar
{
    private Tabs tabs = new Tabs();

    public FloorsGrouping()
    {
        InitializeComponent();
        this.AutoHide = true;
    }

    [Category("Data")]
    [DisplayName("Tabs")]
    [Description("Tabsssssssssssss")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(ItemsCollectionEditor), typeof(UITypeEditor))]
    public Tabs Tab
    {
        get
        {
            //AddTabs();
            return tabs;
        }
        //set
        //{
            //AddTabs();
        //}
    }

    public void AddTabs()
    {
        foreach (DockContainerItem dciItem in Tab)
        {
            if (!Parent.Controls.ContainsKey(dciItem.Name))
            {
                Items.Add(dciItem);
            }
        }
    }
}

[DisplayName("Floors Information")]
[Description("Floors Informationnnnnnnnnnnnnnnn")]
[DefaultProperty("Text")]
[DesignerCategory("Component")]
[ToolboxItem(false)]
public class FloorsInformation : DockContainerItem
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    private SimilarFloorsInformation similarFloorsInformation = new SimilarFloorsInformation();
    private AllFloorsInformation allFloorsInformation = new AllFloorsInformation();
    private string text = "Floors Information";

    public FloorsInformation()
    {

    }

    [Category("Data")]
    [DisplayName("Similar Floors Panel")]
    [Description("Similar Floors Panellllllllllllllllllll")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public SimilarFloorsInformation SimilarFloorsInfo
    {
        get
        {
            return similarFloorsInformation;
        }
        set
        {
            similarFloorsInformation = value;
        }
    }

    [Category("Data")]
    [DisplayName("All Floors Group")]
    [Description("All Floors Groupppppppppppppp")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public AllFloorsInformation AllFloorsInfo
    {
        get
        {
            return allFloorsInformation;
        }
        set
        {
            allFloorsInformation = value;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}

public class Tabs : CollectionBase
{
    public FloorsInformation this[int intIndex]
    {
        get
        {
            return (FloorsInformation)InnerList[intIndex];
        }
        set
        {
            InnerList[intIndex] = value;
        }
    }

    public int Add(FloorsInformation finfItemType)
    {
        return InnerList.Add(finfItemType);
    }

    public bool Contains(FloorsInformation finfItemType)
    {
        return InnerList.Contains(finfItemType);
    }

    public void Remove(FloorsInformation finfItemType)
    {
        InnerList.Remove(finfItemType);
    }

    public void Insert(int intIndex, FloorsInformation finfItemType)
    {
        InnerList.Insert(intIndex, finfItemType);
    }

    public FloorsInformation[] GetValues()
    {
        FloorsInformation[] finfItemType = new FloorsInformation[InnerList.Count];
        InnerList.CopyTo(0, finfItemType, 0, InnerList.Count);
        return finfItemType;
    }
}

顺便说一句,我可以在继承自“CollectionEditor”类的“ItemsCollectionEditor”类的“SetItems”覆盖方法中调用此方法;尽管如此,如果不创建自定义控件类的新实例,我将无法访问“AddTabs”方法。如果我创建自定义控件的新实例,“AddTabs”方法会将更改应用于自定义控件的新控件,而不是 WinForm 中当前添加的自定义控件。

public class ItemsCollectionEditor : CollectionEditor
{
    private Type[] typItems;

    public ItemsCollectionEditor(Type typItem)
        : base(typItem)
    {
        typItems = new Type[] { typeof(FloorsInformation) };
    }

    protected override Type[] CreateNewItemTypes()
    {
        return typItems;
    }

    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm collectionForm = base.CreateCollectionForm();
        collectionForm.Text = "Tabs Collection Editor";
        return collectionForm;
        //return base.CreateCollectionForm();
    }

    protected override object SetItems(object editValue, object[] value)
    {
        return base.SetItems(editValue, value);
    }
}

我必须做什么才能实现我的目标?

4

1 回答 1

1

你有几个选择。

选项1:

如果您只是想FloorsGrouping.Items在设计时公开该属性,您可以将Tab属性的类型更改为SubItemsCollection并返回该Items属性。在这种情况下,您不必担心拦截任何集合更改事件,它会自动为您发生。

[Category("Data")]
[DisplayName("Tabs")]
[Description("Tabsssssssssssss")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(ItemsCollectionEditor), typeof(UITypeEditor))]
public SubItemsCollection Tab {
    get {
        return Items;
    }
}

选项 2:

如果您需要拦截集合更改事件,请将Tabs类修改为继承自ObservableCollection<FloorsInformation>,实现INotifyCollectionChanged.

public class Tabs : System.Collections.ObjectModel.ObservableCollection<FloorsInformation> {
}

在您的FloorsGrouping构造函数中,订阅该CollectionChanged事件。

public FloorsGrouping() {
    ...
    tabs.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(tabs_CollectionChanged);
}

最后,在您的事件处理程序中,处理集合更改。

private void tabs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
    switch (e.Action) {
        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
            foreach (DockContainerItem dciItem in e.NewItems) {
                if (!Parent.Controls.ContainsKey(dciItem.Name))
                    Items.Add(dciItem);
            }
            break;
        case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
            Items.Clear();
            break;
    }
}

对于选项 2,您会注意到CollectionChanged事件是在集合编辑器中进行编辑时实时触发的,而不是在单击 OK 按钮时。但是,当用户最终在集合编辑器中单击“确定”或“取消”按钮时,集合的状态是准确的。

于 2012-11-25T02:51:54.407 回答