Silverlight 3-在items控件中查找元素

Silverlight 3-在items控件中查找元素,silverlight,Silverlight,我有一个绑定到对象集合的ItemsControl。每个对象都有自己的集合以及其他重要属性。要显示对象中的对象,我将在ItemsControl中显示一个树视图。我知道这听起来很疯狂。但是,这只是我试图完成的一个精简版本,以使问题集中在问题上。这是我的样本: <ItemsControl x:Name="myItemsControl"> <ItemsControl.ItemTemplate> <DataTemplate> <contro

我有一个绑定到对象集合的ItemsControl。每个对象都有自己的集合以及其他重要属性。要显示对象中的对象,我将在ItemsControl中显示一个树视图。我知道这听起来很疯狂。但是,这只是我试图完成的一个精简版本,以使问题集中在问题上。这是我的样本:

<ItemsControl x:Name="myItemsControl">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <controls:TreeView x:Name="myTreeView">
      </controls:TreeView>
    </DataTemplate>                                     
  </ItemsControl.ItemTemplate>                                        
</ItemsControl>
上面的代码片段显示了我试图获取TreeView的位置。在itemscontrol中循环遍历项时,如何获取树视图

谢谢大家!

您需要使用和方法迭代视图的子级,直到找到与您的项对应的树。您应该能够针对项目检查
TreeView.DataContext
属性,以验证其是否正确。注意,您需要递归地使用它,因为
GetChild
只检索直接子对象

由于无论如何都需要迭代可视化树,我建议放弃当前的循环,只循环子循环,检查其数据上下文的ID

MyClass instanceToFind = (MyClass)(IdentifyDesiredInstance());                
foreach (MyClass instance in myItemsControl.Items)
{
  if (instance.ID == instanceToFind.ID)
  {
     TreeView treeView = null; // How do I get the TreeView?

     // Do other necessary updates  
  }
}