May 2

Silverlight Dockpanel

Category: Silverlight, WPF

When I started my journey in Silverlight I had already a nice background in WPF technology. For me DockPanel was one of the fundamental containers in WPF and for my suprise it wasn’t included in Silverlight 2 beta release.

I really needed to find one, since for me a lot of layouts are done with it.

I started posting like a mad in the Silverlight forums with the hope of finding an implementation. A guy called Jabb in the forum gave me its implementation and it was a cool start point for the DockPanel.

Jabb’s implementation was not perfect so I still had to struggle a lot in order to have a nice working DockPanel. Thank you Jabb.

The problem was in the measure method, it was not calculating right the space occupied by the child elements.

Basicly, in DockPanel, to calculate it’s measure, we need to determine the width and height of all the elements, taking in account is Dock property.

If someone really needs to understand how that is calculated maybe i can post some more info in the future.

The arrange method is not mine and it was taken from someone WPF lib which i don’t remember now. I’m sorry. I ended up using the this arrange method, because mine was giving problems.

To use this class you will need to add a using line in the XAML side:

xmlns:src=”clr-namespace:NS.SL;assembly=NS.SL”

<src:DockPanel LastChildFill=”True”>
<Button src:DockPanel.Dock=”Top” Width=”50″ Height=”15″ />
<Button src:DockPanel.Dock=”Bottom” Width=”50″ Height=”15″ />
</src:DockPanel>

Altough it still has some bugs, it is completly usable. Just remember to set all the elements with the DockProperty, otherwise it will not work right. If you think you can still put it better, give it a shout. I believe that a fully functional DockPanel will be included in the next release of Silverlight. In the mean while, you can download this one here.

No comments