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 becauseCanvas.Children
does not implementIEnumerable<T>
, but onlyIEnumerable
.You need to reference the
System.Core.dll
assembly in your project, and import theSystem.Linq
namespace in your code file for this to work.
精彩评论