Monday, July 07, 2008

Silverlight Visual Tree Investigation

Wait a moment. Silverlight has Visual and Logical Trees as well as WPF? Not exactly. The design more similar to how it was in WinForms (Parent-Child relationship). But dev team made all possible to make it syntactically similar to WPF. Let’s start

Challenge: Find and hold references to all element, contains text.

Solution: Recursive VisualTreeHelper (introduced in SL2.0 b2)

First of all we should know, that Silverlight is not ready for manufacture, thus direct references might produce memory leaks, thus we’ll use WeakReference to hold pointers to SL objects.

First step: We know for sure, that TextBox and TextBlock controls has Text property, that holds only text. Also TextBlock can hold Inlines collection with Runs and LineBreaks. Let’s find them all

internal static List<WeakReference> GetTextElements(this DependencyObject root)
        {
List<WeakReference> res = new List<WeakReference>();
if (root is TextBlock | root is TextBox)
         {
                res.Add(new WeakReference(root));
         }

Next, we know, that ContentControl can hold text directly inside Content property. We’ll find them too, but should check if it holds text inside…

else if (root is ContentControl)
            {
                (root as ContentControl).CheckAndAddStringContentControl(ref res);
            }

Now ItemsControl. It can hold inside either ContentElement, TextBox, TextBlock or other. So we should check inside it too.

else if (root is ItemsControl)
            {
                ItemsControl ic = root as ItemsControl;
                for (int i = 0; i < ic.Items.Count; i++)
                {
                    if (ic.Items[i] is ContentControl)
                    {
                        (ic.Items[i] as ContentControl).CheckAndAddStringContentControl(ref res);
                    }
                    else
                    {
                        List<WeakReference> tmp = (ic.Items[i] as DependencyObject).GetTextElements();
                        if (tmp != null && tmp.Count > 0)
                        {
                            res.AddRange(tmp);
                        }
                    }
                }
            }

And last, but not least is to dig into all child of each control.

else
            {
                int cnt = VisualTreeHelper.GetChildrenCount(root);

                for (int i = 0; i < cnt; i++)
                {
                    DependencyObject d = VisualTreeHelper.GetChild(root, i);
                    List<WeakReference> tmp = d.GetTextElements();
                    if (tmp != null && tmp.Count > 0)
                    {
                        res.AddRange(tmp);
                    }

                }
            }
            return res;

Step two: Check whether object contains text inside. This one is simple. If not, we’ll call the main method to inter inside the control and check deeper.

internal static void CheckAndAddStringContentControl(this ContentControl cc, ref List<WeakReference> res)
        {
            if (cc.Content is string)
            {
                res.Add(new WeakReference(cc));
            }
            else
                res.AddRange(cc.GetTextElements());
        }

We done. Now we have WeakReferences collection of all items, which contains text in our page.

Have a good day and be nice one to another.

No comments: