简体   繁体   中英

How to loop through UIElement in a List<UIElement>?

I have a List that contains a Canvas which contains also some UIElement as follow:

List<Canvas> cvList = new List<Canvas>();
Canvas cv = new Canvas();
cv.Width = 100;
cv.Height = 100;
Button btn = new Button();
btn.Content = "OK";
cv.Children.Add(btn);
cvList.Add(cv);

The array size of Canvas is dynamic and immmutable, which is the reason I use List instead of Array .

My question is if I were to modify properties of one UIElement (ie Button.Content), how could I access the Button properties? If it was an Array I could simply loop through the array and find an index that matches, like below:

Canvas[] cv = new Canvas[myInt];
Button[] btn = new Button[myInt];
for (int i = 0; i<myInt; i++)
{
cv[i] = new Canvas();
btn[i] = new Button();
btn[i].Content = "OK";
cv[i].Children.Add(btn[i]);
}
btn[5].Content = "Not OK";

but how could I do so for an UIElement that is in a List?

List<UIElement>[int].UIElement.Properties

You would do it the exact same way. You can loop through and find the index that matches.

foreach (Canvas c in cvList) {
    // Do something.
}

This code will work on lists as well as arrays:

for (int i = 0; i<myInt; i++)
{
    cv[i].Content = "Hello";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM