March 13, 2006
01:11 PM

I was playing around with the following DataTemplate to visualise a tree of data.

<DataTemplate DataType="{x:Type l:NodeViewModel}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Column="0" Width="50" Height="50" Fill="OrangeRed" Margin="3" VerticalAlignment="Top" />
        <ItemsControl Grid.Column="1" ItemsSource="{Binding Children}" VerticalAlignment="Top" />
    </Grid>
</DataTemplate>

The idea was to lay out children in rows, whereby children of deeper rows would expand the layout of higher nodes. So given a tree like (A(A(A,B)),C(C)) the result would be:

AAA
B
CC

My data can get deep. Usually this will be no more than 300 to 400 nodes deep, but there is no set limit. Unfortunately, WPF doesn't like deep layout..

"Layout recursion reached allowed limit to avoid stack overflow: '250'. Either the tree contains a loop or is too deep."

While my tree is deep, it isn't very wide - usually only 1-2 nodes. For a short while I thought the simple solution would be to flip the data in the tree, and flip the layout of the DataTemplate. The tree above would become: (A(C,C),A(B),A). But as for the DataTemplate? I don't think I can write it. The layout characteristics that I am looking for seem to depend on the deep layout.

Do I give up on WPF layout and do it myself?

© Douglas Stockwell 2007
Creative Commons License Unless otherwise specified all "source code" examples are available for use under the Creative Commons Attribution-Noncommercial 3.0 License. Please contact me if you would like more flexible licensing terms.
Messenger Presence