Click here to Skip to main content
Click here to Skip to main content

Correcting the WPF Spider control thingy

, 7 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) ).
 

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
double distance =  (bool)node.GetValue(SpiderPanel.IsExpandedProperty) ? CalculateLegDistance(node, children, splitAngle) : 0;
then it looks even cooler.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fredrik Bornander
Software Developer (Senior)
Sweden Sweden
Article videos
Oakmead Apps Android Games
 
21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010
Follow on   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 8 Feb 2011
Article Copyright 2011 by Fredrik Bornander
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid