Reputation: 149
I have created a wpf application ,there are two canvas i would like to store controls of both cancas to one collection so that i can process them without two loop. What is the best method to implement this .
Upvotes: 0
Views: 151
Reputation: 84775
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.
Upvotes: 1