.NET Framework - WPF: Layout recursion reached allowed limit to avoid stack overflow: '255'

Asked By Thorsten Tarrach on 09-Mar-09 04:28 PM

I'm binding my Treeview to an XML document that has less than 100 nested Log
nodes. But I still get this error. If the depth of the XML is 100, how can
the visual tree be 255 nodes deep?
The datatemplate for my Treeview looks like this:

ItemsSource="{Binding XPath=Log}">

Thanks, Thorsten

Andrew Faust replied on 09-Mar-09 04:45 PM
Because your tree nodes are composed of their own nested elements.  Your
template uses a stack panel. That's one layer. Nested inside you have other
controls that have their own content inside them. That gives you a few
layers. The TreeNode itself will be using one or more containers to hold the
collection of child nodes.

Basically this means that each node is probably comprised of 2 - 5 nested
containers regardless of your data template. If you then nest nodes within
nodes to make it 100 deep, you easily exceed 255 nested visual elements.

Andrew Faust
colbert replied on 10-Mar-09 06:14 AM
Hello Thorsten,

Nice to see you again! Andrew's explanation is totally right. Thanks for
Andrew's warmhearted discussion! :-)

Each node will be composed of a few nested visuals meaning that the total
number of nested visuals will reach 255 before too long. This is a known
limitation and reported before. WPF has a hardcoded layout recursion limit
something around 250. We can see the layout recursion limitation from
reflector codes as follows,

static ContextLayoutManager()
s_LayoutRecursionLimit = 250;

The product team was also considering improving this limitation. But the
problem is this is a limitation/performance balance issue. So, at this
time, we choose the 250 limitation since it will insure the application
performance. As the performance get improved in the future version. This
limited value will be increased.

You can get a detailed discussion in the following MSDN Forum thread.

Please let me know if there is any future assistance we can provide from
our side. Have a nice day!

Best regards,
Colbert Zhou (colbertz@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:

Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.
Thorsten Tarrach replied on 14-Mar-09 12:08 PM
Thanks for the explanation Andrew.