Windows phone 7 如何在WindowsPhone7中访问Pivot中的列表框?

Windows phone 7 如何在WindowsPhone7中访问Pivot中的列表框?,windows-phone-7,Windows Phone 7,我有一个Pivot,它的列表框定义为Pivot.ItemTemplate,如下所示 <controls:Pivot x:Name="pivot"> <controls:Pivot.ItemTemplate> <DataTemplate> <ListBox x:Name="listBox"> ... </ListBox> </DataTemplate> </controls:Pivot.It

我有一个Pivot,它的列表框定义为Pivot.ItemTemplate,如下所示

<controls:Pivot x:Name="pivot">
 <controls:Pivot.ItemTemplate>
  <DataTemplate>
   <ListBox x:Name="listBox">
   ...
   </ListBox>
  </DataTemplate>
 </controls:Pivot.ItemTemplate>
</controls:Pivot>
private void Recurse(DependencyObject obj, ref ListBox listBox) { 
 if(obj is ListBox) {
  listBox = obj as ListBox;
  return;
 }

 var count = VisualTreeHelper.GetChildrenCount(obj);
 for(int i=0; i < count; i++) {
  var child = VisualTreeHelper.GetChild(obj, i);
  Recurse(child, ref listBox);
 }
}
然后我创建了一个局部变量来存储ListBox(如果找到了),并递归树视图模型

ListBox listBox = null;
Recurse(pivotItem, ref listBox);
我的递归函数如下所示

<controls:Pivot x:Name="pivot">
 <controls:Pivot.ItemTemplate>
  <DataTemplate>
   <ListBox x:Name="listBox">
   ...
   </ListBox>
  </DataTemplate>
 </controls:Pivot.ItemTemplate>
</controls:Pivot>
private void Recurse(DependencyObject obj, ref ListBox listBox) { 
 if(obj is ListBox) {
  listBox = obj as ListBox;
  return;
 }

 var count = VisualTreeHelper.GetChildrenCount(obj);
 for(int i=0; i < count; i++) {
  var child = VisualTreeHelper.GetChild(obj, i);
  Recurse(child, ref listBox);
 }
}
private void Recurse(DependencyObject obj,ref ListBox ListBox){
如果(对象是列表框){
listBox=obj作为listBox;
返回;
}
变量计数=VisualTreeHelper.GetChildrenCount(obj);
for(int i=0;i
试试:

(Listbox)VisualTreeHelper.GetChild((pivot.SelectedItem as PivotItem), 0); 

看起来这是很久以前的事了,但这对我来说很有用:

首先获取数据透视项:

PivotItem pivotItem = Pivot.ItemContainerGenerator.ContainerFromItem(Pivot.SelectedItem) as PivotItem;
        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++) {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T) {
                return (T)child;
            } else {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }
ListBox listBox = FindFirstElementInVisualTree<ListBox>(pivotItem);
然后从数据透视项获取第一个子项,即列表框:

PivotItem pivotItem = Pivot.ItemContainerGenerator.ContainerFromItem(Pivot.SelectedItem) as PivotItem;
        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++) {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T) {
                return (T)child;
            } else {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }
ListBox listBox = FindFirstElementInVisualTree<ListBox>(pivotItem);
private T findfirstelementvisualtree(DependencyObject parentElement),其中T:DependencyObject{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
如果(计数=0)
返回null;
for(int i=0;i
然后打电话:

PivotItem pivotItem = Pivot.ItemContainerGenerator.ContainerFromItem(Pivot.SelectedItem) as PivotItem;
        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++) {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T) {
                return (T)child;
            } else {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }
ListBox listBox = FindFirstElementInVisualTree<ListBox>(pivotItem);
ListBox ListBox=findfirstelementvisualtree(数据透视项);

在列表框中使用StackPanel 此链接可能对您有所帮助

当我尝试您所说的内容时,我得到一个InvalidOperationException:引用不是有效的可视依赖对象。