WPF Docking Library (转与 The Code Project )

来源:百度文库 编辑:神马文学网 时间:2024/04/30 02:32:43
WPF Docking Library
Byadospace.NET.
A WPF library to easily integrate Windows docking features in applications like VS
C# (C# 2.0)
Windows, .NET (.NET 3.0)
Win32, WPF
Dev
Posted: 15 May 2007
Views: 1,581
Announcements
Vista API competition$10,000 in prizes
Vista Mobile comp:Win a Samsung UMPC
Vista Secure APIcomp: Win a Laptop!
Monthly Competition

Search   Articles Authors   Advanced Search
Sitemap
PrintBroken Article?BookmarkDiscussSend to a friend
9 votes for this article.
Popularity: 4.54. Rating: 4.76 out of 5.
Download source - 43.8 KB
Introduction
Recently I started a project for porting a Windows Forms application to WPF. I was quite a novice in WPF, so at first I was considering to use a some sort of Windows Forms interoperability. In particular, the WinForm application has cool docking functionalities which obviously I wanted to port to a newer version. As I was going deep inside WPF technology I discovered a new world, which radically changed my initial ideas.
In this article I wish to share my first version of a small library which implements, without any win32-interop and in pure WPF, Windows docking feature.
Using the code
There are three fundamental classes. DockManager, Pane and ManagedContent. DockManager is a class responsible to manage main window layout. Pane represents the window area which can be docked to a border. Each Pane contains one or more ManagedContent which precisely refers to a window content of client code. Using this library is straightforward.
DockManager is a user control which can be easy embedded in window. For example, this XAML code creates a DockManager into a DockPanel:
Collapse

Notice that to use DockManager you have to refer a external clr namespace (DockingLibary). Now you can add your windows to DockManager with code like this:
public partial class MainWindow : System.Windows.Window { private TreeViewWindow explorerWindow = new TreeViewWindow(); private OutputWindow outputWindow = new OutputWindow(); private PropertyWindow propertyWindow = new PropertyWindow(); private ListViewWindow listWindow = new ListViewWindow(); public MainWindow() { InitializeComponent(); } private void OnLoaded(object sender, EventArgs e) { dockManager.AddContent(explorerWindow, Dock.Right); dockManager.AddContent(outputWindow, Dock.Bottom); dockManager.AddContent(propertyWindow, Dock.Top); dockManager.AddContent(listWindow, Dock.Left); } }
That‘s all! Windows are initially docked where you set the AddContent member call. The last thing to see is how to add a document window. A document window is a particular window which can‘t be docked to the main window border, but lives only in DocumentsPane which is a particular kind of Pane. The following code adds a document window with a unique title to a DockManager:
private bool ContainsDocument(string docTitle) { foreach (DockingLibrary.DocumentContent doc in dockManager.Documents) if (string.CompareOrdinal(doc.Title, docTitle) == 0) return true; return false; } private void NewDocument(object sender, EventArgs e) { string title = "Document"; int i = 1; while (ContainsDocument(title + i.ToString())) i++; DocumentWindow doc = new DocumentWindow(); doc.Title = title+i.ToString(); dockManager.AddDocumentContent(doc); } Points of Interest
Exactly how is docking realized? I implement a simple algorithm to manage relations between windows which are docked. DockingGrid contains code to organize Pane in a logical tree. DockManager calls DockingGrid.Arrange in order to organize the window layout. You also use DockingGrid.ChangeDock when you need to dock a Pane to a main window border. The following image shows a logical tree of panes (don‘t get confused with WPF logical tree):

Each node is a group of two Panes or a Pane and a child group. To arrange layout, DockingGrid creates a WPF grid for each group with two columns or two rows, depending on split orientation. I hope the image is self-explanatory. Anyway, feel free to ask for more details if you are interested.
History
13/05/07 First preliminary release.
adospace.NET
Clickhere to view adospace.NET‘s online profile.
Other popular Windows Presentation Foundation articles:
Dragging Elements in a Canvas Discusses a class which provides automated dragging of elements in a WPF Canvas.
Creating a look-less custom control in WPF In WPF, custom controls should be look-less. That means, that business logic and UI are strongly separated. When you create a control, you can provide a default template for the control, but anyone who uses the control can override it without touching the business logic.
Advanced Custom TreeView Layout in WPF Reviews an advanced layout customization for the WPF TreeView.
.NET 3.0 WPF Tools && Examples An article which shows how to achieve some of the common XAML concepts, and explains