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

Building a Docking Window Management Solution in WPF

, 1 Jan 2011 CPOL
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.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MixModes.Synergy.VisualFramework.Framework;
using MixModes.Synergy.VisualFramework.Windows;

namespace MixModes.Synergy.VisualFramework.Behaviors
{
    /// <summary>
    /// Illustrates the docking behavior for Content within DocumentContainer
    /// </summary>
    public class ContentDockBehavior : LogicalParentBehavior<DocumentContainer>
    {
        /// <summary>
        /// Dock point
        /// </summary>
        public ContentDockPoint DockPoint
        {
            get;
            set;
        }

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
        /// <exception cref="InvalidOperationException">WindowsManager does not exist in logical tree</exception>        
        protected override void OnAttached()
        {
            base.OnAttached();

            _tabItem = new TabItem();
            _parentStateChangeObserver = new ObservableDependencyProperty(typeof(DocumentContainer), DocumentContainer.StateProperty, OnParentStateChanged);
            _parentStateChangeObserver.AddValueChanged(LogicalParent);
            AssociatedObject.MouseEnter += OnMouseEnter;
            AssociatedObject.MouseUp += OnMouseUp;
            AssociatedObject.MouseLeave += OnMouseLeave;

            // This is important so content dock points can initialize their visibility
            OnParentStateChanged(LogicalParent.State);
        }       

        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            _tabItem = null;
            AssociatedObject.MouseEnter -= OnMouseEnter;
            AssociatedObject.MouseUp -= OnMouseUp;
            AssociatedObject.MouseLeave -= OnMouseLeave;
        }

        /// <summary>
        /// Called when mouse enters the associated object
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void OnMouseEnter(object sender, MouseEventArgs args)
        {
            if (WindowsManager.ActiveWindowsManager == null)
            {
                return;
            }

            switch (DockPoint)
            {
                case ContentDockPoint.Top:
                    IllustrateDocumentDock(Dock.Top, LogicalParent.ActualHeight / 2, double.NaN);
                    break;
                case ContentDockPoint.Left:
                    IllustrateDocumentDock(Dock.Left, double.NaN, LogicalParent.ActualWidth / 2);
                    break;
                case ContentDockPoint.Right:
                    IllustrateDocumentDock(Dock.Right, double.NaN, LogicalParent.ActualWidth / 2);
                    break;
                case ContentDockPoint.Bottom:
                    IllustrateDocumentDock(Dock.Bottom, LogicalParent.ActualHeight / 2, double.NaN);
                    break;
                case ContentDockPoint.Content:
                    IllustrateContentDock();
                    break;
                default:
                    break;
            }
        }        

        /// <summary>
        /// Called when mouse is up on the associated object
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            ClearIllustrations();

            if ((WindowsManager.ActiveWindowsManager != null) &&
                (WindowsManager.ActiveWindowsManager.DraggedPane != null))
            {
                LogicalParent.AddDockPane(DetachDockPaneFromWindowManager(), DockPoint);
            }
        }

        /// <summary>
        /// Called when mouse leaves the associated object
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void OnMouseLeave(object sender, MouseEventArgs args)
        {
            if (WindowsManager.ActiveWindowsManager == null)
            {
                return;
            }

            ClearIllustrations();            
        }

        /// <summary>
        /// Detaches the dock pane from window manager
        /// </summary>
        /// <returns></returns>
        private DockPane DetachDockPaneFromWindowManager()
        {
            DockPane dockPane = WindowsManager.ActiveWindowsManager.DraggedPane;
            WindowsManager.ActiveWindowsManager.RemoveDockPane(dockPane);
            return dockPane;
        }

        /// <summary>
        /// Illustrates the document dock in split windows
        /// </summary>
        /// <param name="dock">The dock.</param>
        /// <param name="height">The height.</param>
        /// <param name="width">The width.</param>
        private void IllustrateDocumentDock(Dock dock, double height, double width)
        {
            if (LogicalParent.DockIllustrationPanel == null)
            {
                return;
            }

            Grid dockPaneIllustratingGrid = new Grid();
            dockPaneIllustratingGrid.Width = width;
            dockPaneIllustratingGrid.Height = height;
            dockPaneIllustratingGrid.Style = WindowsManager.GetDockPaneIllustrationStyle(WindowsManager.ActiveWindowsManager);
            DockPanel.SetDock(dockPaneIllustratingGrid, dock);
            LogicalParent.DockIllustrationPanel.Children.Add(dockPaneIllustratingGrid);
        }

        /// <summary>
        /// Illustrates the content dock
        /// </summary>
        private void IllustrateContentDock()
        {            
            WindowsManager activeWindowsManager = WindowsManager.ActiveWindowsManager;
            _tabItem.Style = WindowsManager.GetDockIllustrationContentStyle(activeWindowsManager);
            LogicalParent.Documents.Add(_tabItem);

            if (LogicalParent.DocumentsTab != null)
            {
                LogicalParent.DocumentsTab.SelectedItem = _tabItem;
            }
        }

        /// <summary>
        /// Clears the illustrations
        /// </summary>
        private void ClearIllustrations()
        {
            if (LogicalParent.DockIllustrationPanel != null)
            {
                LogicalParent.DockIllustrationPanel.Children.Clear();
            }

            LogicalParent.Documents.Remove(_tabItem);            
        }

        /// <summary>
        /// Called when parent state has changed
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnParentStateChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            OnParentStateChanged((DocumentContainerState)e.NewValue);            
        }

        /// <summary>
        /// Called when parent state has changed
        /// </summary>
        /// <param name="state">The state.</param>
        private void OnParentStateChanged(DocumentContainerState state)
        {
            if (IsDocumentStateCompatible(state))
            {
                AssociatedObject.Visibility = Visibility.Visible;
            }
            else
            {
                AssociatedObject.Visibility = Visibility.Hidden;
            }
        }

        /// <summary>
        /// Determines whether document state is compatible with current dockpoint
        /// </summary>
        /// <param name="documentContainerState">State of the document container</param>
        /// <returns>
        /// 	<c>true</c> if document state is compatible with current dockpoint; otherwise, <c>false</c>.
        /// </returns>
        private bool IsDocumentStateCompatible(DocumentContainerState documentContainerState)
        {
            switch (documentContainerState)
            {
                case DocumentContainerState.Empty:
                    return true;
                    break;
                case DocumentContainerState.ContainsDocuments:
                    return (DockPoint == ContentDockPoint.Content);
                    break;
                case DocumentContainerState.SplitHorizontally:
                    return (DockPoint == ContentDockPoint.Left) ||
                           (DockPoint == ContentDockPoint.Right);
                    break;
                case DocumentContainerState.SplitVertically:
                    return (DockPoint == ContentDockPoint.Top) ||
                           (DockPoint == ContentDockPoint.Bottom);
                    break;
                default:
                    break;
            }

            return false;
        }

        // Private members
        private TabItem _tabItem;
        private ObservableDependencyProperty _parentStateChangeObserver;
    }
}

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 | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 1 Jan 2011
Article Copyright 2010 by Ashish Kaila
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid