Tuesday, September 20, 2011

Traversing the Visual Tree in Silverlight

I recently had a problem where I had to find user controls within a ListBox. I was dismayed to find that there were no jQuery or LINQ style methods for traversing the visual tree in Silverlight! (Or at least I couldn't find anything.)


I discovered a workaround here.


In my case, I had to take it a step further, because I wanted all the controls that implemented a particular interface. I added one line of code and now it looks like this:


private void GetChildren(UIElement parent, Type targetType, ref List<UIElement> children)
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    if (count > 0)
    {
        for (int i = 0; i < count; i++)
        {
            UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
            // if the types match or the type implements the interface type
            if (child.GetType() == targetType ||
                child.GetType().GetInterfaces().Contains(targetType))
            {
                children.Add(child);
            }
            GetChildren(child, targetType, ref children);
        }
    }
}
In use it looks like this:

private void SetExpanderStateForCategoryControls(ExpanderState expanderState)
{
    bool isExpanded = false;
    if (expanderState == ExpanderState.Expanded)
        isExpanded = true;
    if (expanderState == ExpanderState.Collapsed)
        isExpanded = false;
 
    List<UIElement> categoryControls = new List<UIElement>();
    GetChildren(GeneralProfileSlotsListBox, typeof(ICategoryControl), ref categoryControls);
    GetChildren(BioProfileSlotsListBox, typeof(ICategoryControl), ref categoryControls);
    foreach (ICategoryControl categoryControl in categoryControls)
    {
        categoryControl.IsExpanded = isExpanded;
    }
}
EDIT: I managed to condense the use case quite a bit. I didn't need to loop thru the list box items to search for the controls I was looking for.

Wednesday, June 15, 2011

Debugger Canvas

Just found this through Reddit, and this is something such that if you're using Visual Studio 2010, you should take a moment and check it out. I can't believe I am so excited about a debugging tool, but this thing is effing brilliant.


"Debugger Canvas is a new user experience for the debugger in Visual Studio Ultimate. It pulls together the code you’re exploring onto a single pan-and-zoom display. As you hit breakpoints or step into code, Debugger Canvas shows just the methods that you’re debugging, with call lines and local variables, to help you see the bigger picture."

Be sure to watch the video, because the description doesn't do it justice.

A few months ago, I had to update a legacy app that seemed to defy our ability to follow the program flow. It took me an entire afternoon to whiteboard the various method calls. Had I had this tool then, I probably could have had it figured out in minutes instead of hours.

I will be the first to admit that I drink deeply from the Microsoft Kool-Aid well, but gosh darnit it's stuff like this that makes me happy to be a .NET programmer.


First Post!

I've been meaning to do this for years, and I'm finally making myself do it. I need a blog for coding, if anything just to remember sometimes the things that are important to me. If they help others, then great!

I'm a web developer who focuses on .NET technology, so most of this blog will about topics surrounding the .NET framework in some manner.