如何从属性=某个值的UIElementCollection中选择一个子节点?

时间:2015-04-02 05:09:14

标签: c# linq children uielementcollection

我有一个UniformGridButtonChildrenButton。每个Tag都有MyUniformGrid.Children.Add(new Button { Margin = new Thickness(5), Tag = Query.GetUInt32("id"), Width = 200 }); 个ID,例如(愚蠢的代码):

Button

如何选择ID为87的子MyUniformGrid.Children.对象? (例如)

当我输入using System.Linq;时(在添加{{1}}之后),Linq方法没有弹出Intellisense。

1 个答案:

答案 0 :(得分:10)

你走了:

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

UIElementCollection implements IEnumerable, not IEnumerable<T>以来,Linq无法直接在MyUniformGrid.Children上投放。因此,OfType<Button>是必需的。