如何从UIElementCollection中选择一个属性=某个值的子级? [英] How to select a child from an UIElementCollection where a property = some value?

查看:170
本文介绍了如何从UIElementCollection中选择一个属性=某个值的子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UniformGrid,其中许多Button都作为Children.每个Button都有一个ID为TagTag. (下沉代码):

I have a UniformGrid with a number of Button's as Children. Each Button has a Tag with an ID, e.g. (dumbed down code):

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

如何选择ID为87的子Button对象? (例如)

How can I select the child Button object with an ID of 87? (as a for instance)

当我键入MyUniformGrid.Children.时(添加using System.Linq;之后),Linq方法不会弹出Intellisense.

Intellisense isn't popping up with the Linq methods when I type MyUniformGrid.Children. (after adding using System.Linq;).

推荐答案

在这里:

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

由于

Linq can't be run directly on MyUniformGrid.Children since UIElementCollection implements IEnumerable, not IEnumerable<T>. Therefore OfType<Button> is required.

这篇关于如何从UIElementCollection中选择一个属性=某个值的子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆