Implementing IComposite

You may implement your own Composite Containers. I recommend merely following the pattern set in the existing Composites?

Here is the IComposite interface:

public interface IComposite
{
        bool IsGrid();
        Panel AddChild<T>(T source, ContentControl chain, int row, int col, FrameworkElement child) where T : FrameworkElement;
        void RemoveFromChain(Border bdr);
        C GetContainer<C>(Border bdr) where C : FrameworkElement;
        C GetContainerFromChildContentControl<C>(ContentControl chain) where C : FrameworkElement;
        T GetParent<T>(ContentControl parentPanel) where T : FrameworkElement;
        T GetParentFromChild<T>(FrameworkElement childElement) where T : FrameworkElement;
        void AddCompositeToContentControl<T>(T chain, ContentControl contentControl, object settings) where T : FrameworkElement;     
        K Get<K, T>(ContentControl chain, int row, int column)
            where K : UIElement
            where T : FrameworkElement;
        Border GetBorder<T>(FrameworkElement childElement);
        UIElementCollection GetChildren(object container);
}

Here is the WrapPanelComposite as a simple example:

public class WrapPanelComposite : IComposite
    {
        public bool IsGrid()
        {
            return false;
        }

        public void RemoveFromChain(Border bdr) //unhook all the children so they may be re-added later, if desired
        {
            GetContainer<WrapPanel>(bdr).Children.Clear();
        }

        public C GetContainer<C>(Border bdr)
            where C: FrameworkElement
        {
           return ((C)(((ContentControl)(bdr.Child)).Content));
        }

        public C GetContainerFromChildContentControl<C>(ContentControl chain) where C : FrameworkElement
        {
           return ((C)(chain.Content));
        }

        public T GetParent<T>(ContentControl sender)
              where T : FrameworkElement
        {
            return (T)((ContentControl)sender).GetParentFromContentControlOfComposite<T>();
        }

        public Panel AddChild<T>(T source, System.Windows.Controls.ContentControl chain, int row, int col, System.Windows.FrameworkElement child)
               where T : FrameworkElement
        {
            WrapPanel g = ((WrapPanel)(chain.Content));
            g.Children.Add(child);

            return (Panel)g;
        }

        public void AddCompositeToContentControl<T>(T chain, ContentControl contentControl, object settings)
            where T: FrameworkElement
        {
            WrapPanel g = new WrapPanel();
            WrapPanelSettings gs = null;

            if (settings != null)
            {
                gs = (settings as WrapPanelSettings);
            }
            else
            {
                if (SettingsManager.ContainsSetting<T>(chain, "WrapPanelSettings"))
                {
                    gs = SettingsManager.GetSetting<T, WrapPanelSettings>(chain, "WrapPanelSettings");
                }
                else
                {
                    //default settings
                    gs = new WrapPanelSettings() { ItemHeight = 15D, ItemWidth = 15D, Orientation = Orientation.Vertical };
                }
            }
            applyWrapPanelSettings(gs, g);

            contentControl.Content = g;     
        }

        private static WrapPanel applyWrapPanelSettings(WrapPanelSettings wSet, WrapPanel w)
        {
            w.ItemHeight = wSet.ItemHeight;
            w.ItemWidth = wSet.ItemWidth;
            w.Orientation = wSet.Orientation;

            return w;
        }

        public T GetParentFromChild<T>(FrameworkElement childElement) where T : FrameworkElement
        {
            return (T)((ContentControl)((WrapPanel)((FrameworkElement)childElement.Parent)).Parent).GetParentFromContentControlOfComposite<T>();
        }

        public System.Windows.Controls.Border GetBorder<T>(System.Windows.FrameworkElement childElement)
        {
            return (Border)((WrapPanel)((FrameworkElement)childElement).Parent).Parent.Cast<ContentControl>().Parent;
        }
        
        public K Get<K, T>(System.Windows.Controls.ContentControl chain, int row, int column)
                where K : UIElement
                where T : FrameworkElement
        {
            K label = default(K);

            label = (K)((WrapPanel)(chain.Content)).Children[column];
            return label;
        }

        public UIElementCollection GetChildren(object container)
        {
            return (container as WrapPanel).Children;
        }
}

Last edited Mar 15, 2014 at 5:12 PM by stagathome0069, version 3

Comments

No comments yet.