Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF
Tip/Trick

Correcting the WPF Spider control thingy

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Feb 2011CPOL2 min read 14K   5   1
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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

Comments and Discussions

 
QuestionThe download link seem to not work, could you please check? Pin
Zoush2-Mar-16 11:28
Zoush2-Mar-16 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.