Question

In silverlight my custom controls are in theUIElementCollection of my StackPanel. I want to get a list of them by a specific value. There only DivElements in the container. It returns Nothing when I know I have one or more. I know I can make a simple loop and cast types inline, but I want to get better with LINQ and Cast(Of TResult). My attempt at casting:

Dim myList = TryCast(spDivs.Children.Where(Function(o) DirectCast(o, DivElement).ElementParent Is bComm).Cast(Of DivElement)(), List(Of DivElement))
Was it helpful?

Solution

The problem is you can't cast into a List(Of DivElement). The collection is a UIElementCollection, not a List(Of T).

You could build a new list, though. This can also be simplified by using OfType instead of casting manually:

Dim myList = spDivs.Children.OfType(Of DivElement)()
                            .Where(Function(o) o.ElementParent Is bComm)
                            .ToList()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top