我创建与C#WPF应用程序:C#WPF我无法进入foreach循环!
我已经有动态生成树状
在这种树状我有一个名为TreeViewItem
BoekenLijst。
那里我生成了动态的TreeViewItem
像“霍比特人”。本书的内容,如“霍比特人”,我存储在Content
属性Checkbox
。
所以树形视图看起来就像这样:
树形目录>
树型视图(BoekenLijst)>
霍比特人
代码完成
我想要把检查的TreeViewItem
个S IN一个ListBox
所以我做了以下内容:
private List<TreeViewItem> GetAllItemContainers(TreeViewItem itemsControl)
{
List<TreeViewItem> allItems = new List<TreeViewItem>();
for (int i = 0; i < itemsControl.Items.Count; i++)
{
// try to get the item Container
TreeViewItem childItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
// the item container maybe null if it is still not generated from the runtime
if (childItemContainer != null)
{
allItems.Add(childItemContainer);
List<TreeViewItem> childItems = GetAllItemContainers(childItemContainer);
foreach (TreeViewItem childItem in childItems)
{
CheckBox checkBoxTemp = childItem.Header as CheckBox;
if (checkBoxTemp != null)
optieListBox.Items.Add(checkBoxTemp.Content);
allItems.Add(childItem);
}
}
}
return allItems;
}
的问题是,它不会进入:的foreach(树型视图childItem在childItems)
有人能帮助我吗?
是它跳过循环?因为这意味着'childItems'是空的。在循环之前放置一个断点并检查childItems的内容。 – 2011-04-08 07:12:16
@bubye,这是你需要找到自己的错误。只有在您尝试追踪问题并失败后才寻求帮助。然后包括你试过的信息。 – 2011-04-08 07:23:17
ChildItems Count = 0,我试过做某些事情,这是我不能出去的问题@Stephen Chung – bubye 2011-04-08 07:24:25