开发者

how can create collection from the controls of two canvas

I have created a wpf application ,there are two canvas i would like to store controls of both cancas to one collection 开发者_Python百科so that i can process them without two loop. What is the best method to implement this .


You could use LINQ's Union operator to pull the two Canvas.Children collections together into one:

for (UIElement child in canvasOne.Children.Cast<UIElement>()
                                     .Union
                       (canvasTwo.Children.Cast<UIElement>()))
{
    ...
}

Note the following:

  • The code shown doesn't actually create a new, mutable collection that you can modify; it merely sets up an IEnumerable<UIElement> such that you can iterate over both collections' elements in one go. That is, the two existing collections will be accessed, not a new one.

  • The Cast<UIElement> operator is necessary because Canvas.Children does not implement IEnumerable<T>, but only IEnumerable.

  • You need to reference the System.Core.dll assembly in your project, and import the System.Linq namespace in your code file for this to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜