简体   繁体   English

编辑用户控件的集合时如何向用户控件添加控件?

[英]How do I add controls to my UserControl when I edit its Collection of controls?

I am using C# in Windows Forms to create a UserControl . 我在Windows窗体中使用C#创建UserControl It's my first .NET UserControl , but I have created many custom components in Delphi in the past, so it's not a totally unfamiliar concept. 这是我的第一个.NET UserControl ,但是过去我在Delphi中创建了许多自定义组件,因此它并不是一个完全陌生的概念。

My new control is a Timeline, similar to those seen in video editing software in which you can place video and audio on various channels. 我的新控件是时间轴,类似于在视频编辑软件中看到的那些,您可以在其中将视频和音频放置在各个频道上。 My control can also contain a number of channels. 我的控件还可以包含多个通道。 I have created a control to act as the basic Timeline, and another control which becomes the Channel when added to the Timeline. 我创建了一个用作基本时间轴的控件,并创建了另一个控件,将其添加到时间轴后成为Channel。 I have created a Collection of Channel objects as a property of the Timeline, and in design mode it gives me a collection editor so that I can add, modify and delete Channels. 我已经创建了Channel对象的集合作为时间轴的属性,在设计模式下,它为我提供了一个集合编辑器,以便我可以添加,修改和删除Channels。 I've made the Channel object Serializeable , and the collection of Channels I create persists in the form on which I have placed the Timeline. 我已经将Channel对象设置为Serializeable ,并且我创建的Channels集合以放置时间轴的形式保留。

What I would like to be able to do is when I exit the Collection editor, the Timeline is updated to display the Channel objects. 我想做的是,当我退出集合编辑器时,时间轴将更新为显示Channel对象。 At present, they exist in the Timeline, but are not being displayed within the Timeline. 目前,它们存在于时间轴中,但未在时间轴中显示。 Obviously, they have to be added to the Controls collection of the Timeline object, but I am at a loss to work out where I should do this. 显然,必须将它们添加到“时间轴”对象的“控件”集合中,但是我不知所措,无法确定应该在哪里进行此操作。 Is there some kind of event raised that says that the Collection has changed, so that I can then go and add or remove the Channels from the displayed Timeline? 是否有某种事件表明收藏集已更改,以便我可以去从显示的时间轴中添加或删除频道?

Here's my code for the Timeline control: 这是我的时间轴控件的代码:

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;
        }
    }
}

And here is the Channel object code. 这是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;
            }
        }
    }
}

Everything else works just fine. 其他一切都很好。 My Timeline control appears in the Toolbox, and I can drop it on my form. 我的时间轴控件出现在工具箱中,我可以将其放在表单上。

When I select the Timeline, its properties are shown, including the Channels property, which appears as a Collection as expected. 当我选择时间轴时,将显示其属性,包括Channels属性,该属性将按预期显示为Collection。 Pressing the [...] button opens a default collection editor (which I might change later), and I can Add and Delete Channels as required. 按下[...]按钮将打开一个默认的集合编辑器(稍后可能会更改),并且我可以根据需要添加和删除通道。 When I close the editor, the Channels exist (I can see the min the form's Designer.cs file), but I want them to appear in the Timeline object. 当我关闭编辑器时,通道存在(我可以在窗体的Designer.cs文件中看到分钟),但是我希望它们出现在时间轴对象中。

So how should I get them added to the Timeline's Controls? 那么我应该如何将它们添加到时间轴的控件中?

Instead of Collection<TimelineChannel> use ObservableCollection<TimeLineChannel> and add handler to it like this 代替Collection<TimelineChannel>使用ObservableCollection<TimeLineChannel>并向其添加处理程序

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择项目时如何更改 UserControl 上的控件背景 - How do I change a controls background on my UserControl when the item is selected 使用绑定到列表 <UserControl> 我该怎么做才能不显示控件 - Using binding to a List<UserControl> how can I do for not showing the controls 如何通过UserControl参数管理控件的宽度? - How do I manage the width of controls through UserControl parameters? 如何将此控件添加到我的项目中? - How can I add this controls to my project? 如何在编辑/更新事件之外访问TextBox控件? - How do I access TextBox controls OUTSIDE of edit/update event? 如何在混合中编辑控件弹出控件的样式 - How do I edit the style of a controls flyout in blend 如何将工具栏数据绑定到各种控件的集合? - How do I databind a toolbar to collection of different kinds of controls? System.Controls.ComboBox - 如何让它添加项目源中不存在的项目? - System.Controls.ComboBox - How do I get it to add an item that does not exist in its Itemssource? 在设计模式下,从背后的用户控件代码中,如何获取父控件中包含的所有控件名称? - From a usercontrol code-behind, in design mode, how do I get all the controls names contained in the parent? 扩展应用程序时,如何防止winforms控件重叠? - How do I keep my winforms controls from overlapping when I expand the application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM