Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Correcting the WPF Spider control thingy

0.00/5 (No votes)
7 Feb 2011 2  
This tip corrects my horrible attempt of a Spider Control
A while ago I wrote the layout logic for a A Spider type control tree thingy for WPF[^] and posted it together with Sacha here on the codeproject. Luckily we posted this when WPF was still uncharted territory for most people so it got some great reviews, even though it is absolutely, 100% the wrong way of implementing something like that.

Yesterday a user commented on it asking for some help on how to extend it and it felt wrong not to help him/her out with a proper version.

The problem with the old one was that it didn't use any standard WPF mechanism for creating a panel. And that's too bad because if one implements a panel using WPF, it's really simple provided you leverage the built in support for layout calculations.

MeasureOverride
Any panel that attempts to place child control according to new rules should override this. In the WPF two-phase layout process this bad boy is used to politely ask the children "If you could have this much space, how much would you like?". And the neat this about this is that the children can come back with a size demand of twice the available. And that's fine, because the second layout pass will give what it thinks is correct, taking into account, but not necessarily adhering to, what the children ask for.

ArrangeOverride
This is the guy actually positioning the children according to what it thinks is correct. For the standard Grid this means looking at attached properties such as Grid.Row and Grid.ColumnSpan. For your own custom panel, this can be whatever you want.


The new attempt
The new attempt at the spider thingy can be downloaded from here[^].

It uses a sounder approach and less code to deliver the same functionality in a simpler and more intuitive way just by doing something as obvious as following the guidelines rather than guessing (which is pretty much what we did for the first attempt, I my defence it was pretty much my first WPF control :) ).


The control is not complete and probably full of bugs because I rushed something together to get the code out early to the commenter asking for help, but it's enough to show how less code, written more according to the guidelines can achieve a more generic and versatile control.

I especially thought it was neat that I could reuse the animation code from here; Animated WPF Panels[^] to animate the spider control.

And if the distance calculation is changed to
C#
double distance =  (bool)node.GetValue(SpiderPanel.IsExpandedProperty) ? CalculateLegDistance(node, children, splitAngle) : 0;

then it looks even cooler.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here