Using DockPanel in WP7
published on: 3/2/2011 | Views: N/A | Tags: CustomControls
by WindowsPhoneGeek
In this article am going to talk about how to use the DockPanel from the Silverlight Toolkit in Windows Phone 7. Recently we received a lot of questions related to DockPanel so in this tutorial I am going to answer some of them.
At first let me mention that DockPanel is not something new for all Silverlight developers because it is a part from the Silverlight Toolkit (NOTE: Silverlight for WP7 Toolkit is something different). Having in mind that Windows Phone 7 is based on Silverlight 3+ it is easy to reuse some pieces of the toolkit code or even the whole code.
Using DockPanel in Windows Phone 7
We have copied the code from the Silverlight Toolkit DockPanel and modified it little to work in Windows Phone 7. So you can use the attached WP7DockPanel.dll assembly at the end of this post.
DockPanel is a layout panel, that provides an easy docking of elements to the left, right, top and bottom of the panel. Basically it arranges child elements around the edges of the panel. Optionally, last added child element can occupy the remaining space. The panel exposes the following properties:
- LastChildFill - This is a dependency property that gets or sets a value indicating whether the last child element added to the DockPanel resizes to fill the remaining space.
- Dock - This is an attached property that determines the dock side of the element. It can be set to one of the following values:Dock.Top, Dock.Bottom, Dock.Left and Dock.Right.
You can use the newly created WP7 DockPanel in this way:
1. Create a sample Windows Phone 7 application project and add reference to WP7DockPanel.dll (NOTE: You can find the assembly attached at the end of this article and the full source code of the DockPanel as well!)
2. Include the following namespane in MainPage.xaml:
xmlns:panel="clr-namespace:WP7DockPanel;assembly=WP7DockPanel"
3.Add the following code into your page to start using the DockPanel:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<panel:DockPanel>
<Button Content="Bottom" panel:DockPanel.Dock="Bottom"/>
<Button Content="Top" panel:DockPanel.Dock="Top"/>
<Button Content="Middle" />
<Button Content="Right" panel:DockPanel.Dock="Right"/>
<Button Content="Left" panel:DockPanel.Dock="Left"/>
</panel:DockPanel>
</Grid>
Here is how the final result should looks like:
How to define DockPanel programmatically
You can define the DockPanel in code behind as well:
DockPanel panel = new DockPanel();
Button btn1 = new Button() { Content = "Bottom" };
btn1.SetValue(DockPanel.DockProperty, Dock.Bottom);
Button btn2 = new Button() { Content = "Top"};
btn2.SetValue(DockPanel.DockProperty, Dock.Top);
Button btn3 = new Button() { Content = "Right" };
btn3.SetValue(DockPanel.DockProperty, Dock.Right);
Button btn4 = new Button() { Content = "Left" };
btn4.SetValue(DockPanel.DockProperty, Dock.Left);
panel.Children.Add(btn1);
panel.Children.Add(btn2);
panel.Children.Add(btn3);
panel.Children.Add(btn4);
this.ContentPanel.Children.Add(panel);
NOTE: However if you decide to port the code on your own you can do this in the following way:
- 1. Download the Silverlight Toolkit source code .
- 2. Go to Controls.Toolkit project
- 3. There you will find a class called DockPanel.cs
- 4. Do not forget to copy the Dock enum
That was all about how to use DockPanel in Windows Phone 7 application. You can find the full source code (WP7DockPanel class library project and the sample test project) as well as the WP7DockPanel.dll here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Dock Middle
posted by: saboor on 11/18/2011 8:16:19 AM
Note : dock middle is on left and left is on middle ?
Something is broken there
posted by: JYA on 11/26/2011 6:58:52 AM
Middle is on the left, when you tap on either Left or Middle, it does as if a big Left button when tap, encompassing both the Left and Middle button
Working Docking Code
posted by: Renan Caldeira on 4/12/2012 3:35:19 PM
The sequence of buttons in source code affects the docking. This code block is OK:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <panel:DockPanel> <Button Content="Bottom" panel:DockPanel.Dock="Bottom"/> <Button Content="Top" panel:DockPanel.Dock="Top"/> <Button Content="Right" panel:DockPanel.Dock="Right"/> <Button Content="Left" panel:DockPanel.Dock="Left"/> <Button Content="Middle" /> </panel:DockPanel>
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
