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.
July 6th, 2008 · Comments (3)
3 Responses to “Silverlight Visual Tree Investigation”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:53 am
Martin Mihaylov on the MultiscaleImage control, Tamir Khason on Visual Tree and thickness, Mike Snow
January 1st, 2009 at 12:53 am
Pingback from Silverlight news for July 7, 2008
January 1st, 2009 at 12:53 am
You’ve been kicked (a good thing) – Trackback from DotNetKicks.com