Click here to Skip to main content
15,892,927 members
Articles / Desktop Programming / WPF

How To Embed An Application Into a Docking Library

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
3 Sep 2011CPOL7 min read 29.3K   2.5K   19  
Step by Step conversion of an application into a Docking application component
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Sofa.Container;
using Sofa.Commons;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace CSWPFMasterDetailBinding
{

    /// <summary>
    /// Interaction logic for HAPSofacontainer.xaml
    /// </summary>
    [Export("Sofa.Examples.SofaComponization.SofaBasicContainer_ViewContract", typeof(UserControl))]
    [ExportMetadata("ProductName", "CSWPFMasterDetailBinding")]
    [ExportMetadata("AvalonContentType", "DocumentContent")]
    [ExportMetadata("AvalonPaneGroup", "RightSide")]
    [ExportMetadata("IfExists_DefaultValue", "DoNotUseIt")]
    [ExportMetadata("ImageURI", "")]
    [ExportMetadata("Label", "CSWPFMasterDetailBinding")]
    [ExportMetadata("DockId", 0)]
    public partial class MainComponent : UserControl,  IBaseContainer, IPartImportsSatisfiedNotification
    {
        //Import sub-components of HAPSofacontainer
        [ImportMany("Sofa.Examples.SofaComponization.MainComponent_ViewContract", AllowRecomposition = true)]
        ExportFactory <UserControl, ISofaComponent_Metadata>[] _views = null;

        //IBaseContainer implementation
        public SofaContainer SofaContainer
        {
            get { return sofaContainer; }
            set { sofaContainer = value; }
        }

        //Not used, required by IBaseContainer
        public SofaMenu SofaMenu
        {
            get { return null; }
            set {  }
        }

        public MainComponent()
        {
            InitializeComponent();
        }

        public void OnImportsSatisfied()
        {
            foreach (var view in _views)
            {
                CmpModel cmp = sofaContainer.RegisterComponent(view);
            }
        }

        //==================== Sofa specific code ==========================================
        //MainComponent is a SofaContainer AND also a Sofa Component

        //The SofaComponent containing this application
        private SofaComponent thisSofaComponent;
        public SofaComponent ThisSofaComponent
        {
            get { return thisSofaComponent; }
            set { thisSofaComponent = value; }
        }

        //The Container (SofaBasicContainer) of this SofaComponent
        private IBaseContainer container;
        public IBaseContainer Container
        {
            get { return container; }
            set
            {
                container = value;
                Container.SofaContainer.SofaCommon += SofaCommonEventHandler;
            }
        }

        public void SofaCommonEventHandler(object sender, SofaEventArgs e)
        {
            if (e.TargetGUID == ThisSofaComponent.GUID)
            {
                if (e.EventType == "PostOpen")
                {
                    //Open the 2 components 
                    sofaContainer.OpenComponent("CustomerList");
                    sofaContainer.OpenComponent("OrderList");

                    //Find the CustomerListComponent and get listViewCustomers as DataContext for this OderListComponent
                    SofaComponent customerComponent = null;
                    SofaComponent orderComponent = null;
                    try
                    {
                        customerComponent = SofaContainer.SofaComponentList.Single(c => c.ProductName == "CustomerList");
                        orderComponent = SofaContainer.SofaComponentList.Single(c => c.ProductName == "OrderList");
                    }
                    catch { }
                    if (customerComponent != null && orderComponent != null)
                        orderComponent.UserControl.DataContext = ((CustomerListComponent)customerComponent.UserControl).listViewCustomers;

                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions