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

北城以北 提交于 2019-11-30 05:30:49

问题


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
});

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

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


回答1:


Here you go:

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.



来源:https://stackoverflow.com/questions/29405363/how-to-select-a-child-from-an-uielementcollection-where-a-property-some-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!