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))
有帮助吗?

解决方案

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()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top