Designer Problem: Collectionelement wird nicht hinzugefügt

Danielku15

Erfahrenes Mitglied
Hi Leute.
Ich arbeite an einem Ribbon Control. Ich Arbeite Gerade an dem ApplicationMenu. Ich habe ein ContainerControl welches mehrere LineRibbonControls beinhalten kann. (Items :LineRibbonControlCollection Eigenschaft).
Nun zu meinem Problem: Beim Designer meines ContainerControls lasse ich über ein Verb ein Component anlegen und dem Container hinzufügen. Jedoch wird im InitializeComponent das Component nicht der Liste hinzugefügt. Wisst ihr wo das Problem liegt? Hier der Code:
C#:
    public class LineRibbonMainMenuControlContainerDesigner : ControlDesigner
    {
        private LineRibbonMainMenuControlContainer _oOwner;

        /// <summary>
        /// Ruft die Auflistung der Komponenten ab, die der durch den Designer verwalteten Komponente zugeordnet ist.
        /// </summary>
        /// <value></value>
        /// <returns>Die Komponenten, die der durch den Designer verwalteten Komponente zugeordnet sind.</returns>
        public override System.Collections.ICollection AssociatedComponents
        {
            get
            {
                return (ICollection)_oOwner.Items;
            }
        }

        /// <summary>
        /// Ruft die Auswahlregeln ab, die die Bewegungsfunktionen einer Komponente angeben. 
        /// </summary>
        public override SelectionRules SelectionRules
        {
            get
            {
                return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
            }
        }

        public override DesignerVerbCollection Verbs
        {
            get
            {
                return new DesignerVerbCollection(new DesignerVerb[] { new DesignerVerb("Add Button", AddButtonHandler), });
            }
        }

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            _oOwner = component as LineRibbonMainMenuControlContainer;
            if (_oOwner == null)
            {
                throw new Exception("The LineRibbonMainMenuControlContainerDesigner can only Design a LineRibbonMainMenuControlContainer. Type was: " + Control.GetType().FullName);
            }
            ISelectionService s = (ISelectionService)GetService(
        typeof(ISelectionService));
            IComponentChangeService c = (IComponentChangeService)
                GetService(typeof(IComponentChangeService));
            s.SelectionChanged += OnSelectionChanged;
            c.ComponentRemoving += OnComponentRemoving;
        }

        private void OnSelectionChanged(object sender, System.EventArgs e)
        {
        }
        private void OnComponentRemoving(object sender, ComponentEventArgs e)
        {
            IComponentChangeService c = (IComponentChangeService)
    GetService(typeof(IComponentChangeService));
            LineRibbonControl button;
            IDesignerHost h = (IDesignerHost)GetService(typeof(IDesignerHost));
            int i;
            // If the user is removing a button
            if (e.Component is LineRibbonControl)
            {
                button = (LineRibbonControl)e.Component;
                if (_oOwner.Items.Contains(button))
                {
                    c.OnComponentChanging(_oOwner, null);
                    _oOwner.Items.Remove(button);
                    c.OnComponentChanged(_oOwner, null, null, null);
                    return;
                }
            }
            // If the user is removing the control itself
            if (e.Component == _oOwner)
            {
                for (i = _oOwner.Items.Count - 1; i >= 0; i--)
                {
                    button = _oOwner.Items[i];
                    c.OnComponentChanging(_oOwner, null);
                    _oOwner.Items.Remove(button);
                    h.DestroyComponent(button);
                    c.OnComponentChanged(_oOwner, null, null, null);
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            ISelectionService s = (ISelectionService)GetService(
            typeof(ISelectionService));
            IComponentChangeService c = (IComponentChangeService)
                GetService(typeof(IComponentChangeService));
            // Unhook events
            s.SelectionChanged -= OnSelectionChanged;
            c.ComponentRemoving -= OnComponentRemoving;
            base.Dispose(disposing);
        }

        private void AddButtonHandler(object sender, EventArgs e)
        {
            LineRibbonButton oButton;
            IDesignerHost h = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerTransaction dt;
            IComponentChangeService c = (IComponentChangeService)
            GetService(typeof(IComponentChangeService));
            // Add a new button to the collection
            dt = h.CreateTransaction("Add Button");
            oButton = (LineRibbonButton)h.CreateComponent(typeof(LineRibbonButton));
            c.OnComponentChanging(_oOwner, null);
            _oOwner.Items.Add(oButton);
            c.OnComponentChanged(_oOwner, null, null, null);
            dt.Commit();
        }

        protected override bool GetHitTest(System.Drawing.Point point)
        {
            point = _oOwner.PointToClient(point);
            foreach (LineRibbonControl oControl in _oOwner.Items)
            {
                if (oControl.Bounds.Contains(point))
                    return true;
            }
            return false;
        }
    }
 
Zurück