【问题标题】:How do I add controls to my UserControl when I edit its Collection of controls?当我编辑控件集合时,如何将控件添加到我的 UserControl?
【发布时间】:2018-09-28 10:26:59
【问题描述】:

我在 Windows 窗体中使用 C# 创建UserControl。这是我的第一个 .NET UserControl,但我过去在 Delphi 中创建了许多自定义组件,所以这并不是一个完全陌生的概念。

我的新控件是一个时间轴,类似于在视频编辑软件中看到的那些,您可以在其中将视频和音频放置在不同的频道上。 我的控件还可以包含多个通道。我创建了一个控件作为基本时间轴,另一个控件在添加到时间轴时成为通道。 我创建了一个 Channel 对象集合作为 Timeline 的属性,在设计模式下,它为我提供了一个集合编辑器,以便我可以添加、修改和删除 Channels。我已经创建了 Channel 对象 Serializeable,并且我创建的 Channels 集合以我放置了 Timeline 的形式存在。

我希望能够在退出集合编辑器时更新时间轴以显示通道对象。目前,它们存在于时间轴中,但并未显示在时间轴中。 显然,它们必须添加到 Timeline 对象的 Controls 集合中,但我不知道应该在哪里执行此操作。是否有某种事件表明集合已更改,以便我可以从显示的时间轴中添加或删除频道?

这是我的时间轴控件代码:

using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace CMSTimeline
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class CMSTimeline : UserControl
    {
        // The collection of Channels
        private Collection<TimelineChannel> channels = new Collection<TimelineChannel>();

        public CMSTimeline()
        {
            InitializeComponent();
        }

        // The property that exposes the collection of channels to the object inspector
        [Category("Data")]
        [Description("The Timeline channels")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Collection<TimelineChannel> Channels
        {
            get { return channels; }
            set { channels = value; }
        }
    }

    class CMSTimelineDesigner : ControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            CMSTimeline uc = component as CMSTimeline;
        }
    }
}

这里是 Channel 对象代码。

using System;
using System.Windows.Forms;

namespace CMSTimeline
{
    [Serializable]
    public partial class TimelineChannel : UserControl
    {
        public TimelineChannel()
        {
            InitializeComponent();
            UICaption.Text = "Channel";
        }

        public TimelineChannel(string aCaption)
        {
            InitializeComponent();
            UICaption.Text = aCaption;
        }

        public string Caption
        {
            get
            {
                return UICaption.Text;
            }
            set
            {
                UICaption.Text = value;
            }
        }
    }
}

其他一切都很好。我的时间轴控件出现在工具箱中,我可以将它放在我的表单上。

当我选择时间轴时,会显示其属性,包括 Channels 属性,它按预期显示为一个集合。 按下 [...] 按钮会打开一个默认的收藏编辑器(我稍后可能会更改),我可以根据需要添加和删除频道。 当我关闭编辑器时,Channels 存在(我可以看到表单的 Designer.cs 文件的最小值),但我希望它们出现在 Timeline 对象中。

那么我应该如何将它们添加到时间轴的控件中?

【问题讨论】:

    标签: c# winforms collections user-controls design-time


    【解决方案1】:

    而不是Collection&lt;TimelineChannel&gt; 使用ObservableCollection&lt;TimeLineChannel&gt; 并像这样向它添加处理程序

    myObservable.CollectionChanged += (sender, e) =>
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (TimeLineChannel c in e.NewItems)
            {
                TimeLine.Controls.Add(c);
            }
        }
    };
    

    【讨论】:

    • 啊!我懂了。 ObservableCollection 公开事件,让其他对象知道某些事情发生了变化。我不知道这种类型。出色的。感谢您的回答。
    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 2015-10-05
    • 2013-03-21
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多