【问题标题】:how to access a ListBox inside a Pivot in windows phone 7?如何在 Windows Phone 7 中访问 Pivot 内的 ListBox?
【发布时间】:2011-10-23 09:37:54
【问题描述】:

我有一个 Pivot,它的 ListBox 定义为其 Pivot.ItemTemplate,如下所示。

<controls:Pivot x:Name="pivot">
 <controls:Pivot.ItemTemplate>
  <DataTemplate>
   <ListBox x:Name="listBox">
   ...
   </ListBox>
  </DataTemplate>
 </controls:Pivot.ItemTemplate>
</controls:Pivot>

如何以编程方式访问与 Pivot.SelectedItem 或 Pivot.SelectedIndex 对应的 ListBox 控件?

我尝试了类似于此链接http://www.windowsphonegeek.com/tips/how-to-access-a-control-placed-inside-listbox-itemtemplate-in-wp7 的内容。

var count = VisualTreeHelper.GetChildrenCount(pivotItem);
for(int i=0; i < count; i++) {
 var child = VisualTreeHelper.GetChild(pivotItem, i);
 if(child is ListBox) {
  //do something
 } else {
  Debug.WriteLine(child.GetType());
 }
}

出于某种原因,我在 Debug.WriteLine 上获得了 System.Windows.Controls.Grid。

我需要获取句柄或访问 Pivot 内的 ListBox(当前显示/选中)的原因是因为我需要重置其视图(将其滚动回顶部)。 ListBox 是绑定到 ObservableCollection 的数据,当我更新集合时,需要将滚动位置放回顶部;否则,一切正常(数据绑定/视觉显示),除了现在视图卡在中间或用户当前所在的位置。如果有更简单的方法可以做到这一点而无需处理 ListBox,我也愿意接受该解决方案。

以防万一有人感兴趣,我进行了修改并想出了一些特别适合我的情况的东西。代码如下。基本上,我必须先获得 PivotItem。

PivotItem pivotItem = pivot.ItemContainerGenerator.ContainerFromItem(myObject) as PivotItem; 

然后我创建了一个局部变量来存储 ListBox(如果找到的话)并递归树视图模型。

ListBox listBox = null;
Recurse(pivotItem, ref listBox);

我的递归函数如下所示。

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

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    试试:

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

    【讨论】:

    • 当我尝试你所说的,我得到一个 InvalidOperationException: Reference is not a valid visual DependencyObject。
    【解决方案2】:

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

    首先获取 PivotItem:

    PivotItem pivotItem = Pivot.ItemContainerGenerator.ContainerFromItem(Pivot.SelectedItem) as PivotItem;
    

    然后从 PivotItem 中获取第一个子项 ListBox:

            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);
    

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多