How to select a child from a UIElementCollection, where property = some value?

I have UniformGridwith a number Buttonlike Children. Each Buttonhas Tagan identifier, for example. (disabled code):

MyUniformGrid.Children.Add(new Button {
    Margin  = new Thickness(5),
    Tag     = Query.GetUInt32("id"),
    Width   = 200
});

How to select child object Buttonwith id 87? (such as,)

Intellisense does not appear using Linq methods on input MyUniformGrid.Children.(after adding using System.Linq;).

+4
source share
1 answer

Here you go:

var MyButton = MyUniformGrid.Children.
               OfType<Button>().
               Single(Child => Child.Tag != null && Child.Tag == 87);

Linq cannot be run directly on MyUniformGrid.Children, because it UIElementCollectionimplements IEnumerable, notIEnumerable<T> . Therefore required OfType<Button>.

+8
source

All Articles