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;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using MixModes.Synergy.VisualFramework.Adorners;
using MixModes.Synergy.VisualFramework.Commands;
using MixModes.Synergy.VisualFramework.Extensions;

namespace MixModes.Synergy.VisualFramework.Windows
{
    /// <summary>
    /// DockPanel class
    /// </summary>
    [TemplatePart(Name = "PART_DOCK_PANE_HEADER", Type = typeof(UIElement))]
    [TemplatePart(Name = "PART_CLOSE", Type = typeof(Button))]
    [TemplatePart(Name = "PART_PIN", Type = typeof(ToggleButton))]
    [TemplatePart(Name = "PART_DOCK_PANE_MENU", Type = typeof(Button))]
    public class DockPane : HeaderedContentControl
    {
        /// <summary>
        /// Initializes the <see cref="DockPane"/> class.
        /// </summary>
        static DockPane()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DockPane), new FrameworkPropertyMetadata(typeof(DockPane)));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DockPane"/> class.
        /// </summary>
        public DockPane()
        {
            CreateCommands();
            Loaded += InitializeDockPane;
        }        

        /// <summary>
        /// Icon Property
        /// </summary>
        public static DependencyProperty IconProperty = DependencyProperty.Register("Icon",
                                                                                    typeof(string),
                                                                                    typeof(DockPane));
        /// <summary>
        /// Condenced dock panel template property
        /// </summary>
        public static DependencyProperty CondencedDockPanelTemplateProperty = DependencyProperty.Register("CondencedDockPanelTemplate",
                                                                                    typeof(DataTemplate),
                                                                                    typeof(DockPane));
        /// <summary>
        /// DockPaneState Property
        /// </summary>
        public static DependencyProperty DockPaneStateProperty = DependencyProperty.Register("DockPaneState",
                                                                                             typeof(DockPaneState),
                                                                                             typeof(DockPane),
                                                                                             new PropertyMetadata(OnDockPaneStateChanged));
        /// <summary>
        /// Close event
        /// </summary>
        public static readonly RoutedEvent CloseEvent =
            EventManager.RegisterRoutedEvent("Close",
                                             RoutingStrategy.Bubble,
                                             typeof(RoutedEventHandler),
                                             typeof(DockPane));

        /// <summary>
        /// Occurs when close button is clicked
        /// </summary>
        public event RoutedEventHandler Close
        {
            add { AddHandler(CloseEvent, value); }
            remove { RemoveHandler(CloseEvent, value); }
        }

        /// <summary>
        /// Toggle pin event
        /// </summary>
        public static readonly RoutedEvent TogglePinEvent =
            EventManager.RegisterRoutedEvent("TogglePin",
                                             RoutingStrategy.Bubble,
                                             typeof(RoutedEventHandler),
                                             typeof(DockPane));

        /// <summary>
        /// Occurs when dock pane's pin is toggled
        /// </summary>
        public event RoutedEventHandler TogglePin
        {
            add { AddHandler(TogglePinEvent, value); }
            remove { RemoveHandler(TogglePinEvent, value); }
        }

        /// <summary>
        /// Header drag event
        /// </summary>
        public static readonly RoutedEvent HeaderDragEvent = 
            EventManager.RegisterRoutedEvent("HeaderDrag",
                                             RoutingStrategy.Bubble,
                                             typeof(MouseButtonEventHandler),
                                             typeof(DockPane));

        /// <summary>
        /// Occurs when header is dragged
        /// </summary>
        public event MouseButtonEventHandler HeaderDrag
        {
            add { AddHandler(HeaderDragEvent, value); }
            remove { RemoveHandler(HeaderDragEvent, value); }
        }

        /// <summary>
        /// Condenced dock panel
        /// </summary>
        public FrameworkElement CondencedDockPanel
        {
            get
            {
                return CondencedDockPaneInstance ?? (CondencedDockPaneInstance = CreateCondencedDockPane());
            }
        }
        
        /// <summary>
        /// Gets or sets the icon.
        /// </summary>
        public string Icon
        {
            get { return GetValue(IconProperty) as string; }
            set { SetValue(IconProperty, value); }
        }

        /// <summary>
        /// Gets or sets a value indicating dock pane state
        /// </summary>
        /// <value>State of dock pane</value>
        public DockPaneState DockPaneState
        {
            get { return (DockPaneState)GetValue(DockPaneStateProperty); }
            set { SetValue(DockPaneStateProperty, value); }
        }

        /// <summary>
        /// Condenced dock panel template
        /// </summary>
        public DataTemplate CondencedDockPanelTemplate
        {
            get { return (DataTemplate) GetValue(CondencedDockPanelTemplateProperty); }
            set { SetValue(CondencedDockPanelTemplateProperty, value); }
        }       

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code 
        /// or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();            
            AttachToVisualTree();
        }

        /// <summary>
        /// Attaches to visual tree to the template
        /// </summary>
        private void AttachToVisualTree()
        {
            AttachDockPaneHeader();
            AttachCloseButton();
            AttachPinButton();            
        }

        /// <summary>
        /// Attaches the dock pane header
        /// </summary>
        private void AttachDockPaneHeader()
        {
            if (DockPaneHeader != null)
            {
                DockPaneHeader.MouseLeftButtonDown -= OnHeaderLeftMouseButtonDown;
                DockPaneHeader.MouseMove -= OnHeaderMouseMove;
            }

            DockPaneHeader = GetTemplateChild("PART_DOCK_PANE_HEADER") as UIElement;

            if (DockPaneHeader != null)
            {
                DockPaneHeader.MouseLeftButtonDown += OnHeaderLeftMouseButtonDown;
                DockPaneHeader.MouseMove += OnHeaderMouseMove;
            }            
        }        

        /// <summary>
        /// Called when left mouse button is down on header
        /// </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 OnHeaderLeftMouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            DraggedPane = this;
            DragStartPoint = e.GetPosition(this);
        }

        /// <summary>
        /// Called when mouse moves on the header
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void OnHeaderMouseMove(object sender, MouseEventArgs e)
        {
            if ((e.LeftButton != MouseButtonState.Pressed) || ((DraggedPane != null) && (DraggedPane != this)))
            {
               return;
            }

            // Check for minimum distance in order to start drag
            Vector distance = e.GetPosition(this) - DragStartPoint;

            if ((Math.Abs(distance.X) < SystemParameters.MinimumHorizontalDragDistance) &&
                (Math.Abs(distance.Y) < SystemParameters.MinimumVerticalDragDistance))
            {
                return;
            }

            RoutedEventArgs args = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left);
            args.RoutedEvent = HeaderDragEvent;
            RaiseEvent(args);
        }

        /// <summary>
        /// Attaches the close button
        /// </summary>
        private void AttachCloseButton()
        {
            if (CloseButton != null)
            {
                CloseButton.Command = null;
            }

            Button closeButton = GetTemplateChild("PART_CLOSE") as Button;
            if (closeButton != null)
            {
                closeButton.Command = CloseCommand;
                CloseButton = closeButton;
            }
        }

        /// <summary>
        /// Attaches the pin button
        /// </summary>
        private void AttachPinButton()
        {
            if (PinButton != null)
            {
                PinButton.Command = null;
            }

            ToggleButton pinButton = GetTemplateChild("PART_PIN") as ToggleButton;
            if (pinButton != null)
            {
                pinButton.Command = TogglePinCommand;
                PinButton = pinButton;
            }
        }

        /// <summary>
        /// Creates the commands
        /// </summary>
        private void CreateCommands()
        {
            CloseCommand = new CommandBase(arg => RaiseEvent(new RoutedEventArgs(CloseEvent)));
            TogglePinCommand = new CommandBase(arg =>
            {
                DockPaneState = PinButton.IsChecked.Value ? DockPaneState.AutoHide : DockPaneState.Docked;
            });
        }

        /// <summary>
        /// Creates the condenced dock pane
        /// </summary>
        private FrameworkElement CreateCondencedDockPane()
        {
            ContentControl content = new ContentControl();
            content.ContentTemplate = CondencedDockPanelTemplate;
            content.Content = new { Header = this.Header, Icon = this.Icon };
            content.DataContext = this;
            return content;
        }

        /// <summary>
        /// Called when state is changed
        /// </summary>
        /// <param name="d">Dependency object</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnDockPaneStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DockPane pane = d as DockPane;
            DockPaneState state = (DockPaneState)e.NewValue;
            OnDockPaneStateChange(pane, state);
        }

        /// <summary>
        /// Initializes the dock pane.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void InitializeDockPane(object sender, RoutedEventArgs e)
        {
            // Refresh state to ensure that proper controls (e.g. resizing adorners) are initialized since
            // before adorner layer was not present.
            OnDockPaneStateChange(this, DockPaneState);
        }

        /// <summary>
        /// Called when dock pane state has changed
        /// </summary>
        /// <param name="pane">Dock pane.</param>
        /// <param name="state">New state (may not be current yet)</param>
        private static void OnDockPaneStateChange(DockPane pane, DockPaneState state)
        {
            switch (state)
            {
                case DockPaneState.Docked:

                    pane.ClearAdornerLayer();

                    if (pane.PinButton != null)
                    {
                        pane.PinButton.Visibility = Visibility.Visible;
                        pane.PinButton.IsChecked = false;
                    }
                    break;

                case DockPaneState.AutoHide:

                    pane.ClearAdornerLayer();

                    if (pane.PinButton != null)
                    {
                        pane.PinButton.Visibility = Visibility.Visible;
                        pane.PinButton.IsChecked = true;
                    }
                    break;

                case DockPaneState.Floating:

                    pane.AddResizingAdorner();

                    if (pane.PinButton != null)
                    {
                        pane.PinButton.Visibility = Visibility.Collapsed;
                    }
                    break;

                case DockPaneState.Content:

                    pane.ClearAdornerLayer();
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// Adds the resizing adorner to the dock pane
        /// </summary>
        private void AddResizingAdorner()
        {
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(this);
            if (layer != null)
            {
                layer.Add(new ResizingAdorner(this));
            }
        }

        /// <summary>
        /// Close command
        /// </summary>
        private ICommand CloseCommand;

        /// <summary>
        /// Pin toggle command
        /// </summary>
        private ICommand TogglePinCommand;

        /// <summary>
        /// Close button
        /// </summary>
        private Button CloseButton { get; set; }

        /// <summary>
        /// Pin button
        /// </summary>
        private ToggleButton PinButton { get; set; }

        /// <summary>
        /// Condenced Dock Pane
        /// </summary>
        private FrameworkElement CondencedDockPaneInstance;

        /// <summary>
        /// Dock pane header
        /// </summary>
        private UIElement DockPaneHeader;

        /// <summary>
        /// Drag start point
        /// </summary>
        private Point DragStartPoint { get; set; }

        /// <summary>
        /// Gets or sets the dragged pane.
        /// </summary>
        /// <value>The dragged pane.</value>
        private static DockPane DraggedPane { get; set; }
    }
}

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
Web03 | 2.8.141220.1 | Last Updated 1 Jan 2011
Article Copyright 2010 by Ashish Kaila
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid