简体   繁体   English

如何在列表中遍历UIElement <UIElement> ?

[英]How to loop through UIElement in a List<UIElement>?

I have a List that contains a Canvas which contains also some UIElement as follow: 我有一个包含Canvas的列表,其中还包含一些UIElement ,如下所示:

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 . Canvas的数组大小是动态且不可改变的,这就是我使用List而不是Array的原因。

My question is if I were to modify properties of one UIElement (ie Button.Content), how could I access the Button properties? 我的问题是,如果要修改一个UIElement属性(即Button.Content),如何访问Button属性? 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? 但是对于列表中的UIElement我该怎么做?

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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