If you regularly read my blog you'll know that I like to flag up those silly little Gotchas that are minor in themselves, but massively frustrating when you come upon them and can't see the cause or the way through. I was working on a project last night and got stuck on something that had me bamboozled for a while. What made it harder to figure out was that I knew I had successfully achieved the result I wanted many times in the past without any problem. Was it me? Or bad syntax (but not spotted by the syntax editor)? Or some bug in the VB2008 installation on this particular machine?
Of course, as usual, it was none of those things.
So, here's the scenario: I had a DockPanel inside a Grid. Inside the DockPanel I wanted to dock a button to the Right side of the DockPanel. Easy Enough? Of course. Just set the attached DockPanel.Dock Property to Right and it should obediently move itself over to the right.
The culprit is in fact the LastChildFill property of the DockPanel. It turns out that by default this is set to True. (I struggle to see the logic of this and would have preferred to see its default as False, but such is life). So what is happening (kinda) is that the button - being the "last item" - is set to fill the DockPanel.
Did you notice that the button has values set for its Height and Width properties? This is significant because it explains why the button remains resolutely in the centre of the panel.

So, what's happening here is that the DockPanel tries to use the "last child", the button - which is of course the only child - to totally fill the DockPanel area. But, because the button only wants itself to have a limit of 23 on its height and 75 on its width, the button has the last word on the matter. Therefore the DockPanel places the button in its centre and begins to stretch it outwards. When it hits the limit imposed by the button it stops stretching it. Logical really and actually is exactly what you would want. If you've set a value on height and width you would expect the project to honour it.
The fix as usual is simple. Change the DockPanel's LastChildFill property to False and all the elements can be laid out as requested: The button has its required height and width and the DockPanel is able to allow it to dock to the right.
