Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given UIElementCollection, find all elements that have StyleA, and change them to StyleB in WPF

I've got a MyGrid.Children UIElementCollection, I would like to find all the Rectangles in it that have there styles set to StyleA, and set them to StyleB.

I'd like to use LINQ if possible, so I can avoid a nasty nested loop.

Something like this pseudocode:

var Recs = from r in MyGrid.Children
                  where r.Style == StyleA && r.GetType() == typeof(Rectangle)
                  select r as Rectangle;

then:

foreach(Rectangle r in Recs)
   r.Style = StyleB;

Can a LINQ guru help me improve my LINQ-fu?

like image 567
Kage Avatar asked Feb 25 '10 20:02

Kage


1 Answers

Your code was almost correct, but UIElements don't have a Style property... You can filter the grid's children based to their type :

var recs = from r in MyGrid.Children.OfType<Rectangle>()
           where r.Style == StyleA
           select r;

foreach(Rectangle r in recs)
   r.Style = StyleB;
like image 169
Thomas Levesque Avatar answered Nov 15 '22 08:11

Thomas Levesque