Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building a Docking Window Management Solution in WPF

, 1 Jan 2011
A docking window solution using WPF as part of Synergy toolkit
Synergy.zip
MixModes.Synergy.Resources
bin
Debug
MixModes.Synergy.Resources.dll
MixModes.Synergy.Resources.pdb
MixModes.Synergy.Resources.csproj.user
MixModes.Synergy.Resources.csproj.vspscc
MixModes.Synergy.Resources.vsdoc
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
GenerateResource.read.1.tlog
MixModes.Synergy.Resources.dll
MixModes.Synergy.Resources.g.resources
MixModes.Synergy.Resources.pdb
MixModes.Synergy.Resources.Resources.Language.resources
TempPE
Properties
Resources
Close.png
DockBottom.png
DockCenter.png
DockHexagon.png
DockLeft.png
DockRight.png
DockTop.png
Error.png
Maximize.png
Minimize.png
NewProject.png
Restore.png
SplitBottom.png
SplitLeft.PNG
SplitRight.png
SplitTop.png
MixModes.Synergy.Themes
bin
Debug
MixModes.Synergy.Resources.dll
MixModes.Synergy.Resources.pdb
MixModes.Synergy.Themes.dll
MixModes.Synergy.Themes.pdb
MixModes.Synergy.Themes.csproj.vspscc
MixModes.Synergy.Themes.vsdoc
obj
Debug
Aero.NormalColor.baml
Brushes.baml
Buttons.baml
DesignTimeResolveAssemblyReferencesInput.cache
DockPane.baml
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
Images.baml
MixModes.Synergy.Themes.dll
MixModes.Synergy.Themes.g.resources
MixModes.Synergy.Themes.pdb
MixModes.Synergy.Themes.Properties.Resources.resources
MixModes.Synergy.Themes_MarkupCompile.cache
ResizableAdorner.baml
ResolveAssemblyReference.cache
TabItems.baml
TempPE
Text.baml
Themes.baml
ToolTips.baml
WindowParts.baml
Windows.baml
Properties
Settings.settings
MixModes.Synergy.Utilities
bin
Debug
MixModes.Synergy.Utilities.dll
MixModes.Synergy.Utilities.pdb
MixModes.Synergy.Utilities.csproj.vspscc
MixModes.Synergy.Utilities.vsdoc
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
MixModes.Synergy.Utilities.dll
MixModes.Synergy.Utilities.pdb
TempPE
Properties
MixModes.Synergy.VisualFramework
Adorners
Behaviors
bin
Debug
de
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
en
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
es
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
fr
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
it
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
ja
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
ko
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
Microsoft.Expression.Interactions.dll
MixModes.Synergy.Resources.dll
MixModes.Synergy.Resources.pdb
MixModes.Synergy.Themes.dll
MixModes.Synergy.Themes.pdb
MixModes.Synergy.Utilities.dll
MixModes.Synergy.Utilities.pdb
MixModes.Synergy.VisualFramework.dll
MixModes.Synergy.VisualFramework.pdb
System.Windows.Interactivity.dll
WPFToolkit.dll
WPFToolkit.pdb
zh-Hans
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
zh-Hant
Microsoft.Expression.Interactions.resources.dll
System.Windows.Interactivity.resources.dll
Commands
Controls
Converters
Extensions
Framework
MixModes.Synergy.VisualFramework.csproj.user
MixModes.Synergy.VisualFramework.csproj.vspscc
MixModes.Synergy.VisualFramework.vsdoc
obj
Debug
Controls
NotificationToolTipContent.baml
DesignTimeResolveAssemblyReferencesInput.cache
MixModes.Synergy.VisualFramework.dll
MixModes.Synergy.VisualFramework.g.resources
MixModes.Synergy.VisualFramework.pdb
MixModes.Synergy.VisualFramework_MarkupCompile.cache
MixModes.Synergy.VisualFramework_MarkupCompile.lref
ResolveAssemblyReference.cache
TempPE
Themes
Generic.baml
Windows
WindowsManager.baml
Properties
Themes
ViewModels
Views
Windows
Synegy
bin
Debug
de
System.Windows.Interactivity.resources.dll
en
System.Windows.Interactivity.resources.dll
es
System.Windows.Interactivity.resources.dll
fr
System.Windows.Interactivity.resources.dll
it
System.Windows.Interactivity.resources.dll
ja
System.Windows.Interactivity.resources.dll
ko
System.Windows.Interactivity.resources.dll
MixModes.Synergy.Resources.dll
MixModes.Synergy.Resources.pdb
MixModes.Synergy.Themes.dll
MixModes.Synergy.Themes.pdb
MixModes.Synergy.Utilities.dll
MixModes.Synergy.Utilities.pdb
MixModes.Synergy.VisualFramework.dll
MixModes.Synergy.VisualFramework.pdb
Synegy.exe
Synegy.pdb
Synegy.vshost.exe
Synegy.vshost.exe.manifest
System.Windows.Interactivity.dll
zh-Hans
System.Windows.Interactivity.resources.dll
zh-Hant
System.Windows.Interactivity.resources.dll
obj
x86
Debug
App.baml
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
MainWindow.baml
ResolveAssemblyReference.cache
Resources
MainWindowMenus.baml
Synegy.exe
Synegy.g.resources
Synegy.pdb
Synegy.Properties.Resources.resources
Synegy_MarkupCompile.cache
TempPE
Views
NewProjectWindow.baml
Properties
Settings.settings
Resources
SplashScreen.jpg
Synegy.csproj.user
Synegy.csproj.vspscc
Synegy.vsdoc
Views
///
/// Copyright(C) MixModes Inc. 2010
/// 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using System.Xml;
using MixModes.Synergy.Utilities;

namespace MixModes.Synergy.VisualFramework.Windows
{
    /// <summary>
    /// Xml window manager deserializer
    /// </summary>
    public class XmlWindowsManagerDeserializer : WindowsManagerDeserializer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlWindowsManagerDeserializer"/> class.
        /// </summary>
        public XmlWindowsManagerDeserializer(Action<DockPane, string> dockPaneReader)
        {
            Validate.NotNull(dockPaneReader, "dockPaneReader");
            _dockPaneReader = dockPaneReader;
        }

        /// <summary>
        /// Reads the windows manager.
        /// </summary>
        protected override void ReadWindowsManager()
        {
            XmlElement windowsManagerElement = _document.DocumentElement;
            Validate.Assert<NullReferenceException>(windowsManagerElement != null);
            Validate.Assert<InvalidOperationException>(windowsManagerElement.Name == "WindowsManager");
            _elementStack.Push(windowsManagerElement);
        }

        /// <summary>
        /// Initializes the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        protected override void InitializeStream(Stream stream)
        {
            _document.Load(stream);
        }

        /// <summary>
        /// Finalizes the deserialization.
        /// </summary>
        protected override void FinalizeDeserialization()
        {
            _elementStack.Pop();
        }

        /// <summary>
        /// Reads the header panes.
        /// </summary>
        /// <param name="dock">Dock point</param>
        /// <returns>DockPanes in order within header pane with specified dock point</returns>
        protected override IEnumerable<DockPane> ReadHeaderPanes(Dock dock)
        {
            XmlElement rootElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(rootElement.Name == "WindowsManager");
            XmlNodeList dockPaneList = rootElement.SelectNodes(string.Format(@"Headers/Header[@Dock='{0}']/DockPane", Enum.GetName(typeof (Dock), dock)));
            foreach (XmlElement dockPaneElement in dockPaneList.OfType<XmlElement>())
            {
                yield return ReadDockPane(dockPaneElement);
            }
        }
       
        /// <summary>
        /// Reads the pinned panes.
        /// </summary>
        /// <param name="dock">Dock point</param>
        /// <returns>DockPanes in order within pinned pane with specified dock point</returns>
        protected override IEnumerable<DockPane> ReadPinnedPanes(Dock dock)
        {
            XmlElement rootElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(rootElement.Name == "WindowsManager");
            XmlNodeList dockPaneList = rootElement.SelectNodes(string.Format(@"PinnedPanes/PinnedPane[@Dock='{0}']/DockPane", Enum.GetName(typeof(Dock), dock)));
            foreach (XmlElement dockPaneElement in dockPaneList.OfType<XmlElement>())
            {
                yield return ReadDockPane(dockPaneElement);
            }
        }

        /// <summary>
        /// Reads the floating panes.
        /// </summary>
        /// <returns>Floating panes</returns>
        protected override IEnumerable<DockPane> ReadFloatingPanes()
        {
            XmlElement rootElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(rootElement.Name == "WindowsManager");
            XmlNodeList dockPaneList = rootElement.SelectNodes(@"FloatingWindows/DockPane");
            foreach (XmlElement dockPaneElement in dockPaneList.OfType<XmlElement>())
            {
                yield return ReadDockPane(dockPaneElement);
            }
        }

        /// <summary>
        /// Reads the root document container and sets the State as well as dimensions for read DocumentContainer
        /// </summary>
        /// <returns>Read document container</returns>
        protected override DocumentContainer ReadRootDocumentContainer()
        {
            XmlElement rootElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(rootElement.Name == "WindowsManager");
            return ReadDocumentContainers().First();
        }

        /// <summary>
        /// Initializes the document container.
        /// </summary>
        /// <param name="documentContainer">The document container.</param>
        /// <returns>Document container state</returns>
        protected override DocumentContainerState InitializeDocumentContainer(DocumentContainer documentContainer)
        {
            ValidateStackNotEmpty();
            
            XmlElement documentContainerElement;
            Validate.Assert<InvalidOperationException>(_documentContainerToDefinitionMapping.TryGetValue(documentContainer, out documentContainerElement) && 
                                                       (documentContainerElement.Name == "DocumentContainer"));

            Grid.SetRow(documentContainer, int.Parse(documentContainerElement.GetAttribute("Row")));
            Grid.SetColumn(documentContainer, int.Parse(documentContainerElement.GetAttribute("Column")));
            
            _elementStack.Push(documentContainerElement);
            
            return (DocumentContainerState)Enum.Parse(typeof(DocumentContainerState), documentContainerElement.GetAttribute("State"));
        }

        /// <summary>
        /// Finalizes the document container.
        /// </summary>
        protected override void FinalizeDocumentContainer()
        {
            FinalizeElement("DocumentContainer");
        }

        /// <summary>
        /// Reads the documents for current document container
        /// </summary>
        /// <param name="documentContainer">The document container.</param>
        protected override void ReadDocuments(DocumentContainer documentContainer)
        {
            ValidateStackNotEmpty();

            XmlElement documentContainerElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(documentContainerElement.Name == "DocumentContainer");

            foreach (XmlElement documentElement in documentContainerElement.SelectNodes(@"Document").OfType<XmlElement>())
            {
                DockPane dockPane = new DockPane();
                _dockPaneReader(dockPane, documentElement.GetAttribute("Data"));
                documentContainer.AddDocument(dockPane);
            }
        }

        /// <summary>
        /// Initializes the split.
        /// </summary>
        /// <param name="parentContainer">The parent container.</param>
        protected override void InitializeSplit(DocumentContainer parentContainer)
        {
            ValidateStackNotEmpty();

            XmlElement documentContainerElement = _elementStack.Peek();
            Validate.Assert<InvalidOperationException>(documentContainerElement.Name == "DocumentContainer");

            XmlElement splitElement = documentContainerElement.FirstChild as XmlElement;
            Validate.Assert<NullReferenceException>(splitElement != null);
            Validate.Assert<InvalidOperationException>(splitElement.Name == "Split");

            _elementStack.Push(splitElement);
        }

        /// <summary>
        /// Finalizes the split.
        /// </summary>
        protected override void FinalizeSplit()
        {
            FinalizeElement("Split");
        }

        /// <summary>
        /// Reads the document containers within current split and sets the State as well as dimensions for read DocumentContainer(s)
        /// </summary>
        /// <returns>Read document containers</returns>
        protected override IEnumerable<DocumentContainer> ReadDocumentContainers()
        {
            ValidateStackNotEmpty();
            XmlElement parentElement = _elementStack.Peek();
            foreach(XmlElement documentContainer in parentElement.SelectNodes("DocumentContainer").OfType<XmlElement>())
            {
                DocumentContainer container = new DocumentContainer();
                _documentContainerToDefinitionMapping[container] = documentContainer;
                yield return container;
            }
        }

        /// <summary>
        /// Reads the dock pane from the xml element
        /// </summary>
        /// <param name="dockPaneElement">The dock pane xml element.</param>
        /// <returns>Newly constructed DockPane</returns>
        private DockPane ReadDockPane(XmlElement dockPaneElement)
        {
            DockPane dockPane = new DockPane();            
            _dockPaneReader(dockPane, dockPaneElement.GetAttribute("Data"));

            double height = double.Parse(dockPaneElement.GetAttribute("Height"));
            dockPane.Height = height;

            double width = double.Parse(dockPaneElement.GetAttribute("Width"));
            dockPane.Width = width;

            XmlAttribute topAttribute = dockPaneElement.Attributes.GetNamedItem("Top") as XmlAttribute;
            if (topAttribute != null)
            {
                Canvas.SetTop(dockPane, double.Parse(topAttribute.Value));
            }

            XmlAttribute leftAttribute = dockPaneElement.Attributes.GetNamedItem("Left") as XmlAttribute;
            if (leftAttribute != null)
            {
                Canvas.SetLeft(dockPane, double.Parse(leftAttribute.Value));
            }

            return dockPane;
        }

        /// <summary>
        /// Validates that the stack is not empty.
        /// </summary>
        private void ValidateStackNotEmpty()
        {
            Validate.Assert<InvalidOperationException>(_elementStack.Count > 0);
        }

        /// <summary>
        /// Finalizes the element.
        /// </summary>
        /// <param name="elementName">Name of the element.</param>
        /// <exception cref="InvalidOperationException">Top element on the stack does not match element name</exception>
        private void FinalizeElement(string elementName)
        {
            ValidateStackNotEmpty();
            Validate.Assert<InvalidOperationException>(_elementStack.Peek().Name == elementName);
            _elementStack.Pop();
        }

        // Private members
        private readonly Action<DockPane, string> _dockPaneReader;
        private readonly Func<string, DocumentContent> _documentReader;
        private readonly Stack<XmlElement> _elementStack = new Stack<XmlElement>();
        private readonly XmlDocument _document = new XmlDocument();
        private readonly Dictionary<DocumentContainer, XmlElement> _documentContainerToDefinitionMapping = new Dictionary<DocumentContainer, XmlElement>();
    }
}

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)

Share

About the Author

Ashish Kaila
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.
 
Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes
 
In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 1 Jan 2011
Article Copyright 2010 by Ashish Kaila
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid