tags:

views:

22

answers:

1

I have the following code which detects all the elements in a Silverlight application beneath a certain point

then filters them to be only those of a particular type - CardButton

IEnumerable<UIElement> elementsBeneathCursor =
                VisualTreeHelper.FindElementsInHostCoordinates(new Point(xPosn, yPosn), Application.Current.RootVisual);
            IEnumerable<CardButton> cardsBeneathCursor = elementsBeneathCursor.OfType<CardButton>();

Even though when I inspect elementsBeneathCursor in the debugger, I can see there are 2 elements of type CardButton Yet when I apply the OfType<> filter the resultant list is null

what's going wrong?

+3  A: 

The resulting list won't actually be null... but the sequence will be empty, if neither of those elements is actually a CardButton. Note that OfType doesn't perform any custom conversions, so if you were expecting those to happen, that may explain it.

Try going through the unfiltered list and printing out the result of calling GetType on each element to see what it really is.

Jon Skeet
ahh - I was being dumb. The source property was null in the debugger, and I needed to expand the results view to have it enumerate the results to be able to show them in the debugger
Christo Fur